1 F 3031 OBJECT ORIENTED PROGRAMMING F 3031
1 F 3031 OBJECT ORIENTED PROGRAMMING F 3031 Object Oriented Programming 1. 0 • Introduction To OOP 2. 0 • Classes And Objects 3. 0 • Inheritance and Polymorphism 4. 0 • Exception handling © 2009 | PN NORHASLIZA BT MUHAMAD NOR
2 1. 0 INTRODUCTION TO OBJECT – ORIENTED PROGRAMMING (OOP) Understand the concepts of Object-oriented Programming © 2009 | PN NORHASLIZA BT MUHAMAD NOR
3 F 3031 OBJECT ORIENTED PROGRAMMING LEARNING OUTCOMES 1 • Define OOP 2 • History Of OOP 3 • Advantages Of Using OOP 4 • Basic Terminologies Of OOP 5 • Abstraction & Encapsulation 6 • Structured & OOP Approach © 2009 | PN NORHASLIZA BT MUHAMAD NOR
4 Define OOP F 3031 OBJECT ORIENTED PROGRAMMING • 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
Cont…. . 5 F 3031 OBJECT ORIENTED PROGRAMMING • 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
6 History Of OOP F 3031 OBJECT ORIENTED PROGRAMMING • ASSGMNT 1 © 2009 | PN NORHASLIZA BT MUHAMAD NOR
7 Advantages Of Using OOP F 3031 OBJECT ORIENTED PROGRAMMING • Code extensibility / kod boleh ditambah q Ability to add codes, which are already existing and consistent with needs. • Code reusability / kod boleh diguna berulangkali q Existing code can be tested first before it is inherited from previous module. q This will save cost and time needed to develop software. q With this method, it will help to increase programmer’s productivity. © 2009 | PN NORHASLIZA BT MUHAMAD NOR
Cont…. . 8 F 3031 OBJECT ORIENTED PROGRAMMING • Represent real world / menggambarkan dunia sebenar q Using object-oriented concept, it will assist the programmer in creating a module because data used represents real situations. • Data security / keselamatan data q The use of encapsulation concept has made data to be protected from misuse by an unauthorized person. © 2009 | PN NORHASLIZA BT MUHAMAD NOR
9 Terminologies Of OOP F 3031 OBJECT ORIENTED PROGRAMMING 1. Object q Object is the term used to explain many things. § Example: student, chair and circle. q An Object consists of data and method q 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. q Method is a set of function that manipulates data, such as method Determine. Status() can determine exam result for object student. Objek : Pelajar Data : nama, alamat, nokadpengenalan, sid, markah, status Method: Menentukan. Status() © 2009 | PN NORHASLIZA BT MUHAMAD NOR
10 Cont…. . F 3031 OBJECT ORIENTED PROGRAMMING 2. Class q A set of objects that have similar attributes(characteristics, which can be seen. ) and methods. q Attributes and methods of a class can be used by each object from that class Student { String name, address, status; int icno, sid; double marks; char Determine. Status() { if marks >= 40 status = “Pass”; else status = “Fail”; } } data class Box { double width, height, depth; data double Compute. Volume() { return( width * height * depth ); } method double Compute. Area() { return( width * height ); method } } Kelas Contoh 2 : Mendefinisikan Kelas Kotak Contoh 1 : Mendefinisikan Kelas Pelajar © 2009 | PN NORHASLIZA BT MUHAMAD NOR
11 Cont…. . F 3031 OBJECT ORIENTED PROGRAMMING 3. Encapsulation q 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. q It enables access to object just by using methods of that object. q It is one of the security features in object-oriented programming (OOP). q 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
12 Cont…. . F 3031 OBJECT ORIENTED PROGRAMMING 4. Data Abstraction q Data abstraction is a process to delete all unnecessary attributes and remain the necessary attributes to describe an object q 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_markah (), Tentu_gred (), Cetak_keputusan () Rajah Menerangkan mengenai konsep pengabstrakan © 2009 | PN NORHASLIZA BT MUHAMAD NOR
13 Cont…. . F 3031 OBJECT ORIENTED PROGRAMMING 5. Inheritance q Create a new class from an existing class together with new attributes and behaviours. q The new class will have the same ability as the base class. q Use the concept of code reusability. Class A Class B Class C Class D Kelas asas (base class) kepada B Kelas terbitan (derived class) kepada A Kelas asas kepada C, D and E 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
Cont…. . 14 F 3031 OBJECT ORIENTED PROGRAMMING 6. Polymorphism q 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. q 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 Differences Between Abstraction & Encapsulation F 3031 OBJECT ORIENTED PROGRAMMING • ASSGMT 1 © 2009 | PN NORHASLIZA BT MUHAMAD NOR
16 Differences Between Structured & OOP Approach F 3031 OBJECT ORIENTED PROGRAMMING 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 Data. Peribadi } Pengaturcaraan Berorientasikan Objek Teknik Pengaturcaraan Berasaskan Objek Penggunaan konsep pengkapsulan yang menggabungkan data dan fungsi dalam satu komponen (kelas) Contoh: class Individu{ ………. . // data void Data. Peribadi( ) }; main( ){ Individu a; /*Objek a capai data dan method dari kelas Individu*/ © 2009 | PN NORHASLIZA BT MUHAMAD NOR }
17 Cont…. . 2 F 3031 OBJECT ORIENTED PROGRAMMING Kod aturcara tidak boleh digunakan berulangkali Membenarkan penggunaan kod yang berulang kali 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. 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 Cont…. . 3 F 3031 OBJECT ORIENTED PROGRAMMING Fungsi Memanipulasikan 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. © 2009 | PN NORHASLIZA BT MUHAMAD NOR 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.
19 Cont…. . 4 F 3031 OBJECT ORIENTED PROGRAMMING Tiada Kawalan Capaian Data Kekangan Dalam Mencapai Data Fungsi main ( ) boleh capai semua data Wujudnya kawalan capaian terhadap data di dan fungsi yang terdapat pada program dalam kelas. Contohnya katakunci private dan protected. Untuk mencapai private data ia mesti dilakukan melalui method ataupun mekanisma pengkapsulan. 5 Tidak menyokong polimorfisma Penggunaan Polimorfisma (polymorphism) Setiap data mesti diisytiharkan terlebih Polimorfisma membolehkan satu fungsi dahulu sebelum operasi ke atasnya dilaksanakan dengan pelbagai kaedah. dilaksanakan. © 2009 | PN NORHASLIZA BT MUHAMAD NOR
- Slides: 19