OBJECT-ORIENTED
PROGRAMMING (OOP)
LECTURE NO. 2
INFORMATION HIDING
• Information is stored within the object
• It is hidden from the outside world
• It can only be manipulated by the object
itself
EXAMPLE – INFORMATION
HIDING
• Ali’s name is stored within his brain
• We can’t access his name directly
• Rather we can ask him to tell his
name
EXAMPLE – INFORMATION
HIDING
• A phone stores several phone numbers
• We can’t read the numbers directly from the SIM
card
• Rather phone-set reads this information for us
INFORMATION HIDING
ADVANTAGES
• Simplifies the model by hiding implementation
details
• It is a barrier against change propagation
ENCAPSULATION
• Data and behaviour are tightly coupled inside an object
• Both the information structure and implementation details of
its
operations are hidden from the outer world
EXAMPLE – ENCAPSULATION
• Ali stores his personal information and knows how to
translate it to the desired language
• We don’t know
• How the data is stored
• How Ali translates this information
EXAMPLE – ENCAPSULATION
• 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
ENCAPSULATION – ADVANTAGES
• Simplicity and
clarity
• Low complexity
• Better understanding
OBJECT HAS AN INTERFACE
• 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
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
IMPLEMENTATION
• Provides services offered by the object
interface
• This includes
• Data structures to hold object state
• Functionality that provides required services
EXAMPLE – IMPLEMENTATION
OF GEAR BOX
• Data Structure
• Mechanical structure of gear
box
• Functionality
• Mechanism to change gear
EXAMPLE – IMPLEMENTATION
OF ADDRESS BOOK IN A PHONE
• Data Structure
• SIM card
• Functionality
• Read/write circuitry
SEPARATION OF INTERFACE &
IMPLEMENTATION
• Means change in implementation does not effect
object interface
• This is achieved via principles of information hiding
and encapsulation
EXAMPLE – SEPARATION OF
INTERFACE &
IMPLEMENTATION
• A driver can drive a car independent of engine type
(petrol, diesel)
• Because interface does not change with the
implementation
EXAMPLE – SEPARATION OF
INTERFACE &
IMPLEMENTATION
• A driver can apply brakes independent of brakes type
(simple, disk)
• Again, reason is the same interface
ADVANTAGES OF SEPARATION
• Users need not to worry about a change until the interface is
same
• Low Complexity
• Direct access to information structure of an object can
produce errors
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