The Difference Between Encapsulation and Information Hiding

LocksIf you ask people familiar with Object Oriented Design which are the most important concepts of the paradigm to them, you will surely hear things like classes, inheritance, polymorphism. Some may talk about genericity; somebody may even mention encapsulation or information hiding.

In fact, many designers use the words encapsulation and information hiding interchangeably.

Encapsulation

In his book  Fundamental Of Object Oriented Design in UML Meilir Page-Jones gives the following definition for encapsulation:

Encapsulation is the grouping of related ideas into one unit, which can thereafter be referred to by a single name.

Single name is here the key word to get an insight of what encapsulation is about: a subroutine (or function as we call it nowadays), for example, is a name that refers to a unit of code. Something that instead of being repeated over and over in the source code is factored out in a single place.

As such the function is the simplest level of encapsulation and abstraction: a designer can refer to it by name as a single entity and even forget the details of the internals and only remember its interface.

Encapsulation in the object oriented world expands on this concept from the subroutine level to the object level: attributes that represents the state of an object and the methods that work on them are packaged into an object type.

Information Hiding

The concept of information hiding is very similar and was first introduced by Parnas in his seminal paper “On the Criteria to Be Used in Decomposing Systems Into Modules” (pdf) almost 40 years ago.

Parnas used the term discussing about which criteria lead to good modularity in structured programming:

every module in the [...] decomposition is characterized by its knowledge of a design decision which it hides from all others. Its interface or definition was chosen to reveal as little as possible about its inner workings.

As you see, here also we talk about some separate entity or module which has some responsibility that we need fulfilled, and we are not interested about how it is fulfilled. But the emphasis here is a bit more about hiding the details from other modules (or objects in the OO world).

Information hiding gives the designer the freedom to modify how the responsibility is fulfilled by a module/object. This is especially valuable at points where the design (or even the requirements) are likely to change.

Uncle Bob Helps Explaining the Difference

Maybe one of the best example explaining the difference between encapsulation and information hiding can be found in Uncle Bob’s most recent book Clean Code (even though the author was not talking about these 2 concepts explicitely).

The following type is merely encapsulated: we have created a concept representing a geometrical concept (the point in a 2D space).

public class Point
{
    public double x;
    public double y;
}

The following type, on the other hand, is an example of information hiding applied to the same concept:

public interface Point
{
    double GetX();
    double GetY();
    void SetCartesian(double x, double y);
    double GetR();
    double GetTheta();
    void SetPolar(double r, double theta);
}

In this second example, the actual implementation is completely hidden: the client doesn’t know whether the internal representation of the point is in rectangular or polar coordinates.

If you are curious and want to know more, you can refer to the following wiki for more opinions on the differences (or lack thereof) between encapsulation and information hiding.

Photo by Leo Reynolds.

kick it on DotNetKicks.com


Shout it

Bookmark and Share

Technorati Tags: , ,
This entry was posted in Design and Patterns and tagged , . Bookmark the permalink.
  • http://www.secretbytes.wordpress.com arjuns

    Thanks for the nice writeup.
    Actually what I usually believed was that, encapsulation is just a way of hiding information, possiblyby using modifiers , like private, protected and internal in(C#) and I was wrong. I never bothered to learn what actually that is. Thanks for the article that made me believe encapsulation is more than just using access modifiers to set their transparency level.

  • http://www.secretbytes.wordpress.com arjuns

    Thanks for the nice writeup.
    Actually what I usually believed was that, encapsulation is just a way of hiding information, possiblyby using modifiers , like private, protected and internal in(C#) and I was wrong. I never bothered to learn what actually that is. Thanks for the article that made me believe encapsulation is more than just using access modifiers to set their transparency level.

  • Pingback: DotNetShoutout

  • maccaroo

    I disagree with the petty distinction made here. Encapsulation refers to enclosing something – separating it from the rest. This strongly implies hiding the internals from the outside world. From the object orientated perspective encapsulation has always been used to with information hiding. In fact, separating the two would leave objects vulnerable to unwanted and unmonitored changes – defeating the whole idea of an ‘object’.

  • Anonymous

    Maccaroo,

    I respect your point of view, and indeed you are not the only one that
    thinks that way (if you follow the link to the c2 wiki page you can see the
    discussion over there). I think we are dealing with nuances here.

    To me, not *all *type of objects need full-protection. Consider for example
    DTOs, or value objects (*struct *in C#) which are often simply containers
    for properties lumped together. To my eyes, those properties are *encapsulated
    *into a type even if no information-hiding is happening.

    Cheers,
    Stefano

  • http://www.innoexcel.com/erpsoftwarecompany.htm ERP Companies

    yes you are saying true………… I am totally agreed..
    Thanks

  • Anonymous

    encapsulation is the better method imho

  • http://wisentechnologies.com/it-courses/java-training.aspx Java Training In Chennai

    Encapsulation & Data Hiding both are different.

  • Pingback: ???? ????? ?? | opentypo