Encapsulation vs Information hiding

user1720616 picture user1720616 · Dec 17, 2012 · Viewed 44.5k times · Source

What exactly are the differences between Ecapsulation and Information Hiding?

Well i know that making fields private and then making setter and getter of the fields is ecapsulation.However does encapsulation mean just this?

suppose i have a class as described below.

public Class IsThisEncapsulation
{
    public int age;

    public void setAge(int age)
    {
       this.age=age;
    }

    public int getAge()
    {
       return age;
    }
}

Now is the class IsThisEncapsulation an example of Encapsulation?

Now would making the field 'age' in the above class private achieve informaton hiding?

Could you please give me clear examples that will help me distinguish these concepts clearly?

Answer

Nandkumar Tekale picture Nandkumar Tekale · Dec 17, 2012

Well I know that making fields private and then making setter and getter of the fields is encapsulation. However, does encapsulation mean just this?

---> Encapsulation is an OOP concept where object state(class fields) and it's behaviour(methods) is wrapped together. Java provides encapsulation using class.

Information Hiding:

--> mechanism for restricting access to some of the object's components. Your above example is the case of Information Hiding if you make age private.


Initially, Information/Data Hiding was considered the part of Encapsulation, and the definitions of Encapsulation would be as:

  • A language mechanism for restricting access to some of the object's components.
  • A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

the second definition is motivated by the fact that in many OOP languages hiding of components is not automatic or can be overridden; thus, information hiding is defined as a separate notion by those who prefer the second definition.

Reference: wikipage