javaoopencapsulationmodularityinformation-hiding

What are the differences between information hiding and encapsulation?


What are the differences between information hiding and encapsulation?

I have read that encapsulation means bundling data and the procedures that should operate on them together. If that is so, does the following class achieve encapsulation?

class IsThisEncapsulation {
    public int age;

    public int getAge() {
        return this.age;
    }

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

Now would declaring the data attribute age private achieve information hiding?


Solution

  • 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:

    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