Chapter 1
Chapter 1
(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.
Behaviour
Attribute/State Identity
Example 1: car
◦ Start engine
◦ Speeding
◦ Change gear
◦ Stop
Example 2: dog
◦ Barking
◦ Fetching
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.
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
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
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: