The Difference Between Encapsulation and Information Hiding

2 minute read

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.

Updated:

Leave a Comment