Characteristics of Object-Oriented
Programming (OOP)
Classes
► Inan OO model, some of the objects exhibit
identical characteristics (information
structure and behaviour)
► We say that they belong to the same class
Example – Class
► Ali
studies mathematics
► Anam studies physics
► Sohail studies chemistry
► Each one is a Student
► We say these objects are instances of the
Student class
Example – Class
► Ahsan teaches mathematics
► Aamir teaches computer science
► Atif teaches physics
► Each one is a teacher
► We say these objects are instances of the
Teacher class
Graphical Representation of Classes
(Class Name)
(Class Name)
(attributes)
Suppressed
(operations)
Form
Normal Form
Major Characteristics of OOP
►Encapsulation
►Inheritance
►Polymorphism
►Abstraction
Encapsulation
► Encapsulation is a mechanism in which data
are encapsulated into an object to protect
data from illegal access or misuse.
► Dataare kept private (hidden) and no direct
access to data is granted.
Inheritance
► Inheritance is a parent-child relationship
between two classes.
►A child inherits characteristics of its parents
► Besides inherited characteristics, a child
may have its own unique characteristics
►
Inheritance in Classes
► If a class B inherits from class A then it
contains all the characteristics (information
structure and behaviour) of class A
► The parent class is called base class and the
child class is called derived class
► Besides inherited characteristics, derived
class may have its own unique
characteristics
Example – Inheritance
Person
Student Doctor
Teacher
Example – Inheritance
Shape
Line Triangle
Circle
Inheritance – “IS A” or
“IS A KIND OF” Relationship
► Eachderived class is a special kind of its
base class
Example – “IS A” Relationship
Person
name
age
gender
eat
walk
Student Teacher Doctor
program designation designation
studyYear salary salary
study teach checkUp
heldExam takeExam prescribe
Example – “IS A” Relationship
Shape
color
coord
draw
rotate
setColor
Circle Triangle
radius Line angle
draw length draw
computeArea draw computeArea
Inheritance – Advantages
► Reuse
► Less redundancy
► Increased maintainability
Polymorphism
► Ingeneral, polymorphism refers to
existence of different forms of a single
entity
► For example, both Diamond and Coal are
different forms of Carbon
Polymorphism in OO Model
► InOO model, polymorphism means that
different objects can behave in different
ways for the same message (stimulus)
► Twodifferent objects respond to the same
request message in their own unique way.
Example – Polymorphism
draw Shape
View
draw
Line Circle Triangle
draw draw draw
Example – Polymorphism
print File
Editor
print
ASCII File PDF File PS File
print print print
Polymorphism – Advantages
► Messagescan be interpreted in different
ways depending upon the receiver class
draw Shape
View
draw
Line Circle Triangle
draw draw draw
Polymorphism – Advantages
► Ingeneral, polymorphism is a powerful tool
to develop flexible and reusable systems
Abstraction
► Abstraction is a way to cope with
complexity.
► Principle of abstraction:
“Capture only those details about an object
that are relevant to current perspective”
Example – Abstraction
Ali is a PhD student and teaches BS
students
► Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Example – Abstraction
Ali is a PhD student and teaches BS
students
► behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Example – Abstraction
Student’s Perspective
► Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Example – Abstraction
Student’s Perspective
► behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Example – Abstraction
Teacher’s Perspective
► Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Example – Abstraction
Teacher’s Perspective
► behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Example – Abstraction
A cat can be viewed with different
perspectives
► Ordinary Perspective ► Surgeon’s Perspective
A pet animal with A being with
Four Legs A Skeleton
A Tail Heart
Two Ears Kidney
Sharp Teeth Stomach
Example – Abstraction
Engineer’s View
Driver’s View
Abstraction – Advantages
► Simplifies the model by hiding irrelevant
details
► Abstraction
provides the freedom to defer
implementation decisions by avoiding
commitment to details