Lecture No.02                    

Lecture Contents

  1. Information Hiding
  2. Encapsulation
  3. Interface
  4. Implementation
  5. Separation of Interface & Implementation
  6. Messages

Information hiding is one of the most important principles of OOP inspired from real life which says that all information should not be accessible to all persons. Private information should only be accessible to its owner.
By Information Hiding we mean “Showing only those details to the outside world which are necessary for the outside world and hiding all other details from the outside world.”

Real Life Examples of Information Hiding

  1. Ali’s name and other personal information is stored in his brain we can’t access this information directly. For getting this information we need to ask Ali about it and it will be up to Ali how much details he would like to share with us.

  1. An email server may have account information of millions of people but it will share only our account information with us if we request it to send anyone else accounts information our request will be refused.

  1. A phone SIM card may store several phone numbers but we can’t read the numbers directly from the SIM card rather phone-set reads this information for us and if the owner of this phone has not allowed others to see the numbers saved in this phone we will not be able to see those phone numbers using phone.

In object oriented programming approach we have objects with their attributes and behaviors that are hidden from other classes, so we can say that object oriented programming follows the principle of information hiding.

In the perspective of Object Oriented Programming Information Hiding is,

“Hiding the object details (state and behavior) from the users”

Here by users we mean “an object” of another class that is calling functions of this class using the reference of this class object or it may be some other program in which we are using this class.

Information Hiding is achieved in Object Oriented Programming using the following principles,

·         All information related to an object is stored within the object
·         It is hidden from the outside world
·         It can only be manipulated by the object itself

Advantages of Information Hiding

Following are two major advantages of information hiding,

It simplifies our Object Oriented Model:

As we saw earlier that our object oriented model only had objects and their interactions hiding implementation details so it makes it easier for everyone to understand our object oriented model.

It is a barrier against change propagation

As implementation of functions is limited to our class and we have only given the name of functions to user along with description of parameters so if we change implementation of function it doesn’t affect the object oriented model.

We can achieve information hiding using Encapsulation and Abstraction, so we see these two concepts in detail now,

Encapsulation means “we have enclosed all the characteristics of an object in the object itself”
Encapsulation and information hiding are much related concepts (information hiding is achieved using Encapsulation)    
We have seen in previous lecture that object characteristics include data members and behavior of the object in the form of functions.

So we can say that Data and Behavior are tightly coupled inside an object and both the information structure and implementation details of its operations are hidden from the outer world.

Examples of Encapsulation

Consider the same example of object Ali of previous lecture we described it as follows,

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,

  1. 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.

  1. 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.

  1. 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.

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

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

  1. 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.

So it has,

·         Data Structure in the form of Mechanical structure of gear box
·         Functionality mechanism to change gear

  1. 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.

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.

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

·         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.

Useful Links:

A Graphical Programming Environment to teach Computer Programming.

Post a Comment

Don't Forget To Join My FB Group VU Vicky

Previous Post Next Post