Object Oriented Programming (CS304) VU
Ali
Characteristics
(attributes)
• Name
• Age
Behavior
(operations)
• Walks
• Eats
You can see that Ali stores his personal information in itself and its behavior is
also implemented in it.
Now it is up to object Ali whether he wants to share that information with
outside world or not. Same thing stands for its behavior if some other object in
real life wants to use his behavior of walking it can not use it without the
permission of Ali.
So we say that attributes and behavior of Ali are encapsulated in it.
Any other object don’t know about these things unless Ali share this information
with that object through an interface,
Same concept also applies to phone which has some data and behavior of
showing that data to user we can only access the information stored in the phone
if phone interface allow us to do so.
Advantages of Encapsulation
The following are the main advantages of Encapsulation,
a. Simplicity and clarity
As all data and functions are stored in the objects so there is no data or function
around in program that is not part of any object and is this way it becomes very
easy to understand the purpose of each data member and function in an object.
b. Low complexity
As data members and functions are hidden in objects and each object has a
specific behavior so there is less complexity in code there will be no such
situations that a functions is using some other function and that functions is
using some other function.
c. Better understanding
Everyone will be able to understand whole scenario by simple looking into object
diagrams without any issue as each object has specific role and specific relation
with other objects.
02.3. Interface
© Virtual University of Pakistan 17
Object Oriented Programming (CS304) VU
Interface is a set of functions of an object that he wants to expose to other objects.
As we discussed previously that data and behavior of each object is hidden in
that object it self so we have to use the concept of interface of the object to expose
its behavior to outer word objects.
• Different objects may need different functions of an object so interface of
an object may be different for different objects.
• Interfaces are necessary for object communication. Each object provides
interface/s (operations) to other objects through these interfaces other
objects communicate with this object.
Example – Interface of a Car
• Steer Wheels
• Accelerate
• Change Gear
• Apply Brakes
• Turn Lights On/Off
Example – Interface of a Phone
• Input Number
• Place Call
• Disconnect Call
• Add number to address book
• Remove number
• Update number
02.4. Implementation
It is actual implementation of the behavior of the object in any Object Oriented
language.
It has two parts,
• Internal data structures to hold an object state that will be hidden from us
it will store values for an object data members.
• Functionality in the form of member functions to provide required
behavior.
Examples of Implementation
a. Gear Box in car system
Consider object Gear Box in car system it has a certain structure and
functionality. When this object will be implemented it will have two things,
• Physical structure of the gear box
• Functionality implemented in this structure to change gear.
Both these things are part of implementation.
18 © Virtual University of Pakistan
Object Oriented Programming (CS304) VU
So it has,
• Data Structure in the form of Mechanical structure of gear box
• Functionality mechanism to change gear
b. Address Book in a Phone
Similarly take the example of contact details saved in the SIM of a phone,
In that case we can say physical structure of SIM card as Data Structure
And Read/write operations provided by the phone as Functionality.
02.5. Separation of Interface & Implementation
As discussed earlier we only show interface of an object to outside world and
hide actual implementation from outside world. The benefit of using this
approach is that our object interface to outside word becomes independent
from inside implementation of that interface.
This is achieved through the concepts of encapsulation and information
hiding.
Real Life example of separation of interface and implementations
¾ Driver has a standard interface to drive a car and using that interface
he drive can drive any car regardless of its model or type whatever
engine type it has or whatever type of fuel it is using.
02.6. Messages
Objects communicate through messages they send messages (stimuli) by
invoking appropriate operations on the target object. The number and kind of
messages that can be sent to an object depends upon its interface
Examples – Messages
A Person sends message (stimulus) “stop” to a Car by applying brakes
A Person sends message “place call” to a Phone by pressing appropriate button
02.7. Summary
• Information hiding is achieved through encapsulation.
• Encapsulation and Information Hiding are related to each other.
• Interface of an object provides us the list of available functions.
• An object may have more than one interface.
• Interface and implementation are separated from each other to achieve
Information Hiding.
• Objects communicate with each other using messages.
© Virtual University of Pakistan 19
Object Oriented Programming (CS304) VU
Useful Links:
http://www.alice.org/
A Graphical Programming Environment to teach Computer Programming.
20 © Virtual University of Pakistan
Object Oriented Programming (CS304) VU
Lecture No.03
Lecture Contents:
• Abstraction
• Classes
• Inheritance
• Major benefits of inheritance (Reuse)
03.1. Abstraction
Real life objects have a lot of attributes and many kind of behaviors but most of the
time we are interested in only that part of the objects that is related to the problem
we are currently going to solve, for example in implementing a school system we
don’t need to take care of the personnel life of a student or a teacher as it will not
effect our system in any way so we will see these objects in the perspective of school
system and will ignore their other characteristics, this concept is called “Abstraction”.
Abstraction is a way to cope with complexity and it is used to simplify things.
Principle of abstraction:
“Capture only those details about an object that are relevant to current perspective”
Abstraction Example:
Suppose we want to implement abstraction for the following statement,
“Ali is a PhD student and teaches BS students”
Here object Ali has two perspectives one is his student perspective and second is his
teacher perspective.
We can sum up Ali’s attributes as follows,
Name
Age
Student Roll No
Year of Study
CGPA
Employee ID
Designation
Salary
As you can see out of all these listed attributes some belong to Ali’s student
perspective(Roll No, CGPA, Year of study) and some belong to Ali’s teacher
perspective(Employee ID, Designation, Salary).
Similarly we can sum up Ali’s behavior as follows,
Study
DevelopExam
© Virtual University of Pakistan 21
Object Oriented Programming (CS304) VU
GiveExam
TakeExam
PlaySports
Eat
DeliverLecture
Walk
As was the case with attributes of object Ali, its behavior can also be divided in Ali’s
student perspective as well as Ali’s teacher perspective.
Student’s Perspective
Attributes:
- Name - Employee ID
- Student Roll No - Designation
- Year of Study - Salary
- CGPA - Age
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
Behaviour:
- Study - DevelopExam
- GiveExam - TakeExam
- PlaySports - Eat
- DeliverLecture - Walk
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
22 © Virtual University of Pakistan
Object Oriented Programming (CS304) VU
Two Ears Kidney
Sharp Teeth Stomach
A car can be viewed with different perspectives
Driver’s View Engineer’s View
Abstraction – Advantages
Abstraction has following major advantages,
1. It helps us understanding and solving a problem using object oriented
approach as it hides extra irrelevant details of objects.
2. Focusing on single perspective of an object provides us freedom to change
implementation for other aspects of for an object later.
Similar to Encapsulation Abstraction is also used for achieving information hiding as
we show only relevant details to related objects, and hide other details.
03.2. Classes
In OOP we create a general sketch for each kind of objects and then we create
different instances using this sketch we call this sketch or prototype or map as
“class”.
All objects of same kind exhibit identical characteristics (information structure and
behavior) however they have data of their own.
Class –Example 1
Consider the objects given below,
• Ali studies mathematics
• Anam studies physics
• Sohail studies chemistry
Each one is a Student so we say these objects are instances of the Student class.
Class –Example 2
Consider the objects given below,
© Virtual University of Pakistan 23
Object Oriented Programming (CS304) VU
• Ahsan teaches mathematics
• Aamir teaches computer science
• Atif teaches physics
Each one is a teacher so we say these objects are instances of the Teacher class
Class Representation:
we can represent a class using a rectangle as follows,
(Class Name)
(Attributes)
(Operations)
(Class Name)
Normal Form Suppressed Form
Class Example: Circle
Circle
center
radius
draw
computeArea
Circle
Normal Form Suppressed Form
Class Example: Person
24 © Virtual University of Pakistan
Object Oriented Programming (CS304) VU
Person
name
age
gender
eat
walk
Person
Normal Form Suppressed Form
03.3. Inheritance
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
Person
Student Doctor
Teacher
Shape
Line Triangle
Circle
Inheritance – “IS A” or “IS A KIND OF” Relationship
© Virtual University of Pakistan 25