October 27, 2013

Information Hiding and Encapsulation in OOP

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. Your name and other personal information is stored in your brain we can’t access this information directly. For getting this information we need to ask you about it and it will be up to you how much details you 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.


  1. what the different between encapsulation and information hiding. I see both are the same.

    1. Encapsulation and information hiding are much related concepts (information hiding is achieved using Encapsulation).

    2. AOa Bor! Information hiding is a main topic or heading which hides the important information of an object. We can achieved information hiding by encapsulation and Abstraction.
      When we talk about Encapsulation-> we enclosed all characteristic or attribute of an object in the object itself. So we can understand encapsulation cope with only attribute or characteristic which or same thing.
      Interface: it is a set of functions that we use to expose an object behavior or operation to other object.

      So we can say encapsulation holds attribute and interface holds functionality of an object or behaviour of of an object.

      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.

      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.

  2. good descriprtion......
    what about abstration.........with recpect to these two concept?

  3. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | list of java training institutes in chennai

  4. thanks for sharing this great article.

  5. BlueHost is the best web-hosting company for any hosting services you need.

  6. I usually see this blog. I like to read articles articles in this blog. Features of Object Oriented Programming

  7. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.

    Hadoop Training in chennai

  8. This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up Java online training Bangalore


C program to Read From a File

#include <stdio.h> #include <stdlib.h> void main() {     FILE *fptr;     char filename[15];     char ch;   ...