You are on page 1of 19

1

1.0 2.0 3.0 4.0

Introduction To OOP

Classes And Objects Inheritance and Polymorphism Exception handling

2009 | PN NORHASLIZA BT MUHAMAD NOR

1 2 3 4 5 6

Define OOP History Of OOP


v nt es Of Usin OOP

B sic Terminolo ies Of OOP bstr ction & Enc psul tion Structure & OOP ppro ch

2009 | PN NORHASLIZA BT MUHAMAD NOR

Programming paradigm that uses objects" data structures consisting of datafields and methods and their interactions to design applications and computer programs. A type of programming where the programmer can define data types and types of operations, which will be performed on the data structure. Example: Data Structure Data Student No. Mark data types Integer float Types of operation: Student No. : Sort student list based on Student No. Mark : Calculate mark.
2009 | PN NORHASLIZA BT MUHAMAD NOR

By using this method, data structure will become an object such as Student object, which consists of data and functions. The programmer can create relationship between one object with another object such as an object inherits characteristics from another object Object-oriented programming is a method used to write programs where data and behaviours are packaged together in one class. Object is a member of a class.

2009 | PN NORHASLIZA BT MUHAMAD NOR

ASSGMNT 1

2009 | PN NORHASLIZA BT MUHAMAD NOR

Code extensibility / kod boleh ditambah


Ability to add codes, which are already existing and consistent with needs.

Code reusability / kod boleh diguna berulangkali


Existing code can be tested first before it is inherited from previous module. This will save cost and time needed to develop software. With this method, it will help to increase programmers productivity.

2009 | PN NORHASLIZA BT MUHAMAD NOR

Represent real world / menggambarkan dunia sebenar


Using object-oriented concept, it will assist the programmer in creating a module because data used represents real situations.

Data security / keselamatan data


The use of encapsulation concept has made data to be protected from misuse by an unauthorized person.

2009 | PN NORHASLIZA BT MUHAMAD NOR

1. Object
Object is the term used to explain many things.  Example: student, chair and circle. An Object consists of data and method Properties of an object are called data. In the real world, characteristics of an object can be divided into two types:  Data that can be seen such as a human with two hands.  Data that cannot be seen such as a human with a name. Method is a set of function that manipulates data, such as method DetermineStatus() can determine exam result for object student.
Objek : Pelajar Data : nama, alamat, nokadpengenalan, sid, markah, status Method: MenentukanStatus()

2009 | PN NORHASLIZA BT MUHAMAD NOR

10

2. Class
A set of objects that have similar attributes(characteristics, which can be seen.) and methods. Attributes and methods of a class can be used by each object from that class.

class Student { String name, address, status; int icno, sid; double marks; char DetermineStatus() { if marks >= 40 status = Pass; else status = Fail; } } Kelas

data method

class Box { double width, height, depth; double ComputeVolume() { return( width * height * depth ); } double ComputeArea() { return( width * height ); }

data

method
} Kelas

Contoh 2 : Mendefinisikan Kelas Kotak Contoh 1 : Mendefinisikan Kelas Pelajar

2009 | PN NORHASLIZA BT MUHAMAD NOR

11

3. Encapsulation
Encapsulation is a process of tying together all data and methods that form a class and control the access to data by hiding its information. It enables access to object just by using methods of that object. It is one of the security features in object-oriented programming (OOP). Attributes and methods of a class can be used by each object from that class.
Class Student Name, Student ID, Address, IC No

Calculate_result() Determine_grade() Print_result() Figure above Explains the concept of encapsulation in OOP for class Student

Based from the example given, data and methods are combined in one class. If the college management wants to get the status whether a student pass or fail , they only have to know the status without knowing how to determine or calculate the grade. So, this is a way of implementing encapsulation where the code in the program is hidden thus to prevent from being modified.
2009 | PN NORHASLIZA BT MUHAMAD NOR

4. Data Abstraction
Data abstraction is a process to delete all unnecessary attributes and remain the necessary attributes to describe an object Attributes and methods of a class can be used by each object from that class.

Objek Pelajar

Kelas Pelajar Sifat/(Attribute)

Pengabstrakan

Nama, ID pelajar, Alamat, No KP

Kelakuan/ (Behaviors) Kira_marka (), Tentu_gre (), Cetak_keputusan ()

Rajah Menerangkan mengenai konsep pengabstrakan


2009 | PN NORHASLIZA BT MUHAMAD NOR

13

5. Inheritance
Create a new class from an existing class together with new attributes and behaviours. The new class will have the same ability as the base class. Use the concept of code reusability.
Class A Kelas asas (base class) kepada B Kelas terbitan (derived class) kepada A Kelas asas kepada C, D and E

Class B

Class C

Class D

Class E

Kelas terbitan kepada B

Rajah : Hubungan di antara satu kelas dengan kelas lain. Kelas terbitan boleh mewarisi ciri-ciri Kelas asas.
2009 | PN NORHASLIZA BT MUHAMAD NOR

14

6. Polymorphism
Polymorphism is a process of giving the same message to two or more different objects and produce different behaviours depending on how the objects receive the message. It is said to be One interface, many methods.

Contoh 1:
Mesej: Keluarkan wang anda dari bank: Objek Tindakan Pelajar 1 : Menggunakan mesin ATM yang disediakan oleh pihak bank di mana akaun dibuka Pelajar 2 : Menggunakan mesin ATM (MEPS) dari bank lain

2009 | PN NORHASLIZA BT MUHAMAD NOR

15

ASSGMT 1

2009 | PN NORHASLIZA BT MUHAMAD NOR

16

Pengaturcaraan Berstruktur 1 Berasaskan Fungsi Yang didefinisikan Pengaturcara Memecahkan satu program yang besar pada beberapa fungsi. Setiap fungsi akan melaksanakan tugas yang lebih spesifik Contoh: void main( ){ .. Peribadi (nama,umur); } void Peribadi(char *a, int b) { //Definisi fungsi DataPeribadi }

Pengaturcaraan Berorientasikan Objek Teknik Pengaturcaraan Berasaskan Objek Penggunaan konsep pengkapsulan yang menggabungkan data dan fungsi dalam satu komponen (kelas) Contoh: class Individu{ .. // data

void DataPeribadi( ) }; main( ){ Individu a; /*Objek a capai data dan method dari kelas Individu*/

2009 | PN NORHASLIZA BT MUHAMAD NOR

17

Kod aturcara tidak boleh digunakan berulangkali Setiap fungsi ditugaskan untuk satu tugas yang spesifik. Kita mesti menerima fungsi itu sebagaimana ia ditulis. Untuk mengubahsuainya, kod itu haruslah disalin semula dan diubah untuk memenuhi keperluan. Contoh: Kita telah merekacipta satu radio untuk kegunaan rumah. Teknologi yang digunakan itu boleh digunakan untuk mencipta radio kereta ataupun radio yang digunakan di tepi pantai.

Membenarkan penggunaan kod yang berulang kali Ia boleh dilakukan melalui teknik pewarisan. Teknik ini membolehkan objek untuk mewarisi ciri-ciri (fungsi dan data) objek lain. Contoh: Kita menggunakan radio kegunaan rumah yang telah dicipta untuk digunakan pada kereta dan di pantai

2009 | PN NORHASLIZA BT MUHAMAD NOR

18

Fungsi Memanipulasikan Data Data (cth: nama, umur) akan dihantar ke fungsi-fungsi tertentu (cth: cetak, papar, tambah, dan padam) untuk dimanipulasikan. Fungsi yang akan memanipulasikan data tidak ditetapkan terlebih dahulu.

Menggunakan Pengkapsulan Untuk Bertindak Ke atas Data Pengkapsulan digunakan untuk mempakej data bersama fungsi yang akan bertindak ke atas data. Ia mengenalpasti fungsi yang akan dilaksanakan ke atas setiap objek. Kelas akan mengawal sebarang operasi keatas data dan fungsi yang berada didalamnya.

2009 | PN NORHASLIZA BT MUHAMAD NOR

19

Tiada Kawalan Capaian Data Fungsi main ( ) boleh capai semua data dan fungsi yang terdapat pada program

Kekangan Dalam Mencapai Data Wujudnya kawalan capaian terhadap data di dalam kelas. Contohnya katakunci private dan protected. Untuk mencapai private data ia mesti dilakukan melalui method ataupun mekanisma pengkapsulan.

Tidak menyokong polimorfisma Setiap data mesti diisytiharkan terlebih dahulu sebelum operasi ke atasnya dilaksanakan.

Penggunaan Polimorfisma (polymorphism) Polimorfisma membolehkan satu fungsi dilaksanakan dengan pelbagai kaedah.

2009 | PN NORHASLIZA BT MUHAMAD NOR