Introduction to Object Oriented Programming
(OOP)
   Object     oriented    programming     (OOP) is   a
    programming technique that uses “objects” to design
    the applications and computer programs.
   In real life, things that you see such as cars, trees, cats,
    mobile phones and so on are objects. Even, you as a
    student is an object.
   An object combines data and operations on that data
    into a single unit
   Object is a thing, both tangible or intangible, that
    have properties/attributes (data values) and
    behaviors (methods) that manipulate the data.
   Object does thing and usually depends on its
    properties
   Object is called an instance of a class
                      Object can perform actions and can
                         have actions performed on it
                                   Behaviour
        Attribute/State                                     Identity
                                                           Object can be called and
   Properties used to                                        used as single unit
defined characteristics of
       the object
                                  OBJECT
   The relevant data that relate with the object
   Attribute also can describe about state of object
   In OOP, this attribute will be declared as variable
   Example:
         Object       Attribute           Value
                   Colour         Blue
                   Category       sport
                   Manufacturer   Proton
          Car
                        State             Value
                   Current gear   Gear 4
                   Engine         Off
   Possible operations / actions to be performed on data
   Example 1: car
    ◦   Start engine
    ◦   Speeding
    ◦   Change gear
    ◦   Stop
   Example 2: dog
    ◦ Barking
    ◦ Fetching
   Also known as methods or functions in programming
      Cat1:
        Attribute:
         • Colour: white
         • EyeColour: black
         • WeightKg: 4
        Behaviours:
         • playing, fighting, hunting
Cat   Cat2:
        Attribute:
          • Colour: black
          • EyeColour: green
          • WeightKg: 3.5
        Behaviours:
        • playing, eating
      Attribute/state:
      • Colour: white
      • Manufacturer: Nissan
      • Model: Grand Livina
      • Type: MPV
Car
      Behaviours :
      • Moving, speeding
   Identify the object(s) and the attributes for the
    following scenario.
The Perlis Library is a public library that stores
various books that can be borrowed. The library is
open to both members and non-members.
   Identify the object(s) and the attributes for the
    following scenario.
Company XYZ Sdn. Bhd sells their very own
product which is printer. There are many types of
printers sold by the company. The printers are
identified by their printer ID, brand, colour, price and
printer type.
   Identify the object(s) and the attributes for the
    following scenario.
As a programmer, you are asked to develop a
program for keeping track of stock in a grocery
store. The stock consists of variety of products.
Each product has product ID, product name, price,
quantity, and supplier name.
   Identify the object(s) and the attributes for the
    following scenario.
Athletes of SUKMA Games at Perlis 2018 may
involve in more than one game. For example, the
athletes who involved in 100 meters games may
involve in marathon and high jumping. The athlete
must make sure that the games are not clashing
each other in terms of the date and game venue.
   Identify the object(s) and the attributes for the
    following scenario.
Academic Affairs Department has been asked to
trace the UiTM graduates employability. Among the
important information for that purpose are the
duration of getting job after graduate and the
relevancy of the job with their field of studies.
   An object is defined by a class
   A class is the blueprint of an object
   The class uses methods to define the behaviors of
    the object
   The class that contains the main method of a Java
    program represents the entire program
   A class represents a concept, and an object
    represents the example of that concept
   Multiple objects can be created from the same
    class
                                        object
   a kind of template.        • a thing, both tangible
   represents the common        and intangible.
    structure & behaviour      • is comprised of data
    shared by the same           & operations that
    type.                        manipulate these
   A collection of objects of   data.
    similar type.              • is called an instance
   must be defined before       of a class.
    creating an instance of
    the class.
 Class
STUDENT
                            Variables
  stuID                     – represent the data/attributes
stuName                     - set of properties
 stuProg
  setID()                   Methods
setName()                   – represent the behaviors
 setProg()                  - a sequence of instructions that class or
                            object needs to follow
                   Object
                :STUDENT
                stuID : “1234”            Object or instance of the class
             stuName : “Ahmad”            that has the property values
              stuProg : “CS110”
   List the attributes and behaviours of the following
    objects:
    a)   Rectangle
    b)   Book
    c)   Employee
    d)   Fee
   List the objects that can be instantiate from the following
    class:
    a) Shape
    b) Animal
    c) Assessment
    d) Address
    e) Person
   Save development time (and cost) by reusing code
    ◦ once an object class is created it can be used in other
      applications
    Easier debugging
    ◦ classes can be tested independently
    ◦ reused objects have already been tested
   Abstraction
   Encapsulation
   Inheritance
   Polymorphism
   Abstraction ~ designing classes
    ◦ The creation of a software module contain relevant
      characteristics of the object to be represented
    ◦ Focus only on the important facts about the problem at
      hand
    ◦ Shows only important things to the user and hides the
      internal details.
    ◦ to design, produce, and describe so that it can be
      easily used without knowing the details of how it
      works.
Real life example of abstraction:
Example 1:
Driving a car
 When you drive a car, you don’t have to know how the
  gasoline and air are mixed.
 Instead you only have to know how to use the controls.
Example 2:
ATM Machine
❑ People can perform ATM operations like cash withrawal,
  money transfer, check balance, etc, but we can’t know
  internal details about ATM.
   Data Abstraction
    ◦ It is a process of hiding the data
    ◦ In OOP, this is implemented automatically while writing
      the code in the form of class and object.
•   It is a mechanism that binds together code and the data
    it manipulates and keeps both safe from the outside
    interference and misuse.
•   It is a process of wrapping of data and methods in a
    single unit.
•   Access to the code & data inside the wrapper is tightly
    controlled with the help of modifiers like private, public,
    protected etc.
•   In Java, encapsulation can be achieve using class
    keyword.
 Also known as data hiding
 Only object’s methods can modify information in the
 object.
 Advantage: to secure the data from other methods and
 control the access to the data.
Example:
public class EncapTest{
                                   All the data members
      private String name;         (variables) are declared
      private String idNum;        as private. So, only the
                                   member in the class can
      private int age;             access      the   private
}                                  member.
   Inheritance ~ the use of extends
    ◦ Ability to create new classes from existing classes
   The process by which one class acquires the properties
    of another class.
   The class which give the properties is known as base or
    super or parent class.
   An object need only to define all those properties that
    make it unique within its class. It inherits its general
    attributes from its parent.
   Inheritance is also known as re-usability or extendable
    classes.
   Classes with properties in common can be grouped so
    that their common properties are only defined once.
    Superclass – inherit its attributes & methods to the
    subclass(es).
   Subclass – can inherit all its superclass attributes &
    methods besides having its own unique attributes &
    methods.
   Polymorphism
    ◦ From the Greek, meaning “many forms” (poly – many,
      morphs – forms).
    Real life example of polymorphism
    A person:
    ◦ In classroom, behave like a student
    ◦ At home, behave like son/daughter
    ◦ In shopping mall, behave like a customer
    ◦ In bus, behave like a passenger
   Polymorphism in programming
    ◦ One single statement is used as a call to various
      methods
    ◦ The exact method to be called is only know during
      runtime
    ◦ A feature that allows one interface to be used for a
      general class of actions.
       “one interface, multiple methods”
    ◦ It is the use of abstract classes and virtual methods
    ◦ Can be applied in the overloaded methods (a few
      methods that have the same name but with different
      parameters).