1 Oop Concepts

Embed Size (px)

Citation preview

  • 7/30/2019 1 Oop Concepts

    1/19

    Introduction To OOP1.0

    Classes And Objects2.0

    Inheritance and Polymorphism3.0

    Exception handling4.0

    1

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 7/30/2019 1 Oop Concepts

    2/19

    2

  • 7/30/2019 1 Oop Concepts

    3/19

    Define OOP1

    History Of OOP2

    Advantages Of Using OOP3

    Basic Terminologies Of OOP4

    Abstraction & Encapsulation5

    Structured & OOP Approach6

    3

    2009 | PN NORHASLIZA BT MUHAMAD NOR

  • 7/30/2019 1 Oop Concepts

    4/19

    A type of programming where the programmer can define data types and types ofoperations, which will be performed on the data structure.

    Example:Data data types

    Data Structure Student No. Integer

    Mark float

    Types of operation:

    Student No. :

    Sort student list based on Student No.

    Mark :

    Calculate mark.

    2009 | PN NORHASLIZA BT MUHAMAD NOR

    4

  • 7/30/2019 1 Oop Concepts

    5/19

    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

    5

  • 7/30/2019 1 Oop Concepts

    6/19

    ASSGMNT 1

    2009 | PN NORHASLIZA BT MUHAMAD NOR

    6

  • 7/30/2019 1 Oop Concepts

    7/19

    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

    7

  • 7/30/2019 1 Oop Concepts

    8/19

    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

    8

  • 7/30/2019 1 Oop Concepts

    9/19 2009 | PN NORHASLIZA BT MUHAMAD NOR

    9

    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, statusMethod: MenentukanStatus()

  • 7/30/2019 1 Oop Concepts

    10/19 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

    Contoh 1 : Mendefinisikan Kelas Pelajar

    data

    method

    Contoh 2 : Mendefinisikan Kelas Kotak

    class Box

    { double width, height, depth;

    double ComputeVolume()

    { return( width * height * depth ); }

    double ComputeArea()

    { return( width * height ); }

    }

    data

    method

    Kelas

  • 7/30/2019 1 Oop Concepts

    11/19 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.

  • 7/30/2019 1 Oop Concepts

    12/19

    2009 | PN NORHASLIZA BT MUHAMAD NOR

    12

    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

    PengabstrakanNama, ID pelajar,

    Alamat, No KP

    Sifat/(Attribute)

    Kira_markah (),Tentu_gred (),

    Cetak_keputusan ()

    Kelakuan/ (Behaviors)

    Kelas Pelajar

    Rajah Menerangkan mengenai konsep

    pengabstrakan

  • 7/30/2019 1 Oop Concepts

    13/19

    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

    Class B

    Class C

    Kelas asas (base class)

    kepada B

    Kelas terbitan (derived class)

    kepada A

    Kelas asas kepada C, D and E

    Kelas terbitan kepada B

    Class EClass D

    Rajah : Hubungan di antara satu kelas dengan

    kelas lain. Kelas terbitan boleh

    mewarisi ciri-ciri Kelas asas.

  • 7/30/2019 1 Oop Concepts

    14/19

    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 TindakanPelajar 1 : Menggunakan mesin ATM yang disediakan oleh pihak

    bank di mana akaun dibuka

    Pelajar 2 : Menggunakan mesin ATM (MEPS) dari bank lain

  • 7/30/2019 1 Oop Concepts

    15/19

    ASSGMT 1

    2009 | PN NORHASLIZA BT MUHAMAD NOR

    15

    6

  • 7/30/2019 1 Oop Concepts

    16/19

    2009 | PN NORHASLIZA BT MUHAMAD NOR

    16

    Pengaturcaraan BerstrukturPengaturcaraan Berorientasikan Objek

    1 Berasaskan Fungsi Yang didefinisikanPengaturcaraMemecahkan satu program yang besar pada

    beberapa fungsi. Setiap fungsi akan

    melaksanakan tugas yang lebih spesifikContoh:

    void main( ){

    ..Peribadi (nama,umur);}void Peribadi(char *a, int b){ //Definisi fungsi DataPeribadi

    }

    Teknik Pengaturcaraan Berasaskan Objek

    Penggunaan konsep pengkapsulan yang

    menggabungkan data dan fungsi dalam

    satu komponen (kelas)

    Contoh:

    class Individu{.. // datavoid DataPeribadi( )};main( ){

    Individu a;/*Objek a capai data dan method

    dari

    kelas Individu*/}

  • 7/30/2019 1 Oop Concepts

    17/19

    2009 | PN NORHASLIZA BT MUHAMAD NOR

    17

    2 Kod aturcara tidak boleh digunakanberulangkali

    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 yangberulang 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

    8

  • 7/30/2019 1 Oop Concepts

    18/19

    2009 | PN NORHASLIZA BT MUHAMAD NOR

    18

    3 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 mempakejdata 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.

  • 7/30/2019 1 Oop Concepts

    19/19

    2009 | PN NORHASLIZA BT MUHAMAD NOR

    19

    4 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.

    5 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.