OOP Lecture 01 03
OOP Lecture 01 03
Architectural models
Mechanical models
lives-in
Objects Ali House
Ali drives
House
Car Car Tree
Tree
Interactions
Ali lives in the house
Ali drives the car
People think in terms of objects
State (attributes)
Well-defined behaviour (operations)
Unique identity
State (attributes)
Name
Age
behaviour (operations)
Walks
Eats
Identity
His name
State (attributes)
- Color
- Model
behaviour (operations)
- Accelerate - Start Car
- Change Gear
Identity
- Its registration number
State (attributes)
- Hours - Seconds
- Minutes
behaviour (operations)
- Set Hours - Set Seconds
- Set Minutes
Identity
- Would have a unique ID in the model
State (attributes)
- Year - Day
- Month
behaviour (operations)
- Set Year - Set Day
- Set Month
Identity
- Would have a unique ID in the model
Encapsulation/Data Hiding
Polymorphism
Inheritance
Data is stored within the object
We don’t know
How the data is stored
How Ali translates this information
A Phone stores phone numbers in digital
format and knows how to convert it into
human-readable characters
We don’t know
How the data is stored
How it is converted to human-readable characters
Simplicity and clarity
Low complexity
Better understanding
An object encapsulates data and behaviour
So how objects interact with each other?
Each object provides an interface
(operations)
Other objects communicate through this
interface
Steer Wheels
Accelerate
Change Gear
Apply Brakes
Turn Lights On/Off
Input Number
Place Call
Disconnect Call
Add number to address book
Remove number
Update number
Provides services offered by the object
interface
This includes
Data structures to hold object state
Functionality that provides required services
Data Structure
Mechanical structure of gear box
Functionality
Mechanism to change gear
Data Structure
SIM card
Functionality
Read/write circuitry
Means change in implementation does not
effect object interface
Low Complexity
Principle of abstraction:
Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Ali is a PhD student and teaches BS students
behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Student’s Perspective
Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Student’s Perspective
behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
Teacher’s Perspective
Attributes
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
Teacher’s Perspective
behaviour
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
A cat can be viewed with different
perspectives
Driver’s View
Simplifies the model by hiding irrelevant
details
Suppressed
(operations)
Form
Normal Form
Circle
center Circle
radius
draw Suppressed
computeArea Form
Normal Form
Person
name Person
age
gender Suppressed
eat Form
walk
Normal Form
A child inherits characteristics of its parents
Student Doctor
Teacher
Shape
Line Triangle
Circle
Each derived class is a special kind of its base
class
Person
name
age
gender
eat
walk
Circle Triangle
radius Line angle
draw length draw
computeArea draw computeArea
Reuse
Less redundancy
Increased maintainability
Main purpose of inheritance is reuse
We can easily add new classes by inheriting
from existing classes
Select an existing class closer to the desired
functionality
Create a new class and inherit it from the selected
class
Add to and/or modify the inherited functionality
Shape
color
coord
draw
rotate
setColor
Circle Triangle
radius Line angle
draw length draw
computeArea draw computeArea
Person
name
age
gender
eat
walk