18

I have got two definitions of encapsulation which could not fit into one definition.

  1. Encapsulation is data hiding. With the use of private, protected and public, pack the data into single component.
  2. Whatever changes encapsulate it. Protecting anything which is prone to change.

How these two definitions are talking about the same concept?

Himanshu Yadav
  • 13,315
  • 46
  • 162
  • 291
  • 6
    The second isn't a definition, it is a rule of thumb when to use encapsulation.... – Willem Van Onsem Feb 19 '15 at 17:07
  • 4
    I do not agree with this. Abstraction is completely different concept. – Himanshu Yadav Feb 19 '15 at 17:07
  • 1
    @CommuSoft Then what is the relationship between these two arguments. I mean how hiding data is same as modularizing changing code. (Apologies for my english) – Himanshu Yadav Feb 19 '15 at 17:10
  • @JarrodRoberson Can you please provide the link for the duplicate question? I could not find this question all over the internet. – Himanshu Yadav Feb 20 '15 at 14:06
  • (The link to the duplicate as at the top.) I agree that these are not duplicates: this question is encapsulation vs protecting changeable code, the other is encapsulation vs abstraction. You could try changing the title to make that clearer. – mk. Feb 20 '15 at 15:43

5 Answers5

24

Encapsulation is probably the most misunderstood concept of OOP.

Encapsulation is NOT data hiding!

"Encapsulation" comes from "capsule". It means putting things together, closing them in a package, and the "things" we are talking about here are data and functions. Programming without encapsulation means that functions dealing with data are "floating around", somewhere in your code, and though they deal with your data and even take that particular type as input, they are separated from your data.

Let me make an example without focusing on "public" and the like: if you have a class that deals with complex numbers, which have a real and imaginary part, you could simply define it like this:

class complex {
    double real;
    double imaginary;
};

With the old, pre-encapsulation style that was used for example in C, to get the absolute value of this number you would define a function like this:

double absolute(double real, double imaginary);

And this wouldn't be connected to the class at all! Of course you could also define a function that takes a class complex as input, but it would still be an external function. Thus, to use it you would have to do this:

complex A;
A.real = 1;
A.imaginary = -3;

and to get the absolute value you would have to call

absolute(A.real, A.imaginary);

Instead, you could use encapsulation and put data and functions together:

class complex {
    double real;
    double imaginary;
    double absolute();  // inside the class, encapsulated into it!
};

and then to get the absolute value you would simply have to call the method like

A.absolute();

This doesn't require data hiding at all. The advantage is that the code is more manageable, because you can clearly see all the related "things" (that is, data and functions) grouped together, so at a glance you know what you have (data) and what you can do with it (methods).

Information hiding wouldn't be possible without this, because it means that you limit access to some members (the private ones) from the outside, therefore you must have some methods inside or you wouldn't be able to do anything with your data!

At the same time, information hiding helps putting encapsulation to good use: if people could access data from the outside, there would be a very high danger to have other coders that write their own (not encapsulated) code to deal with your data, which would at the very least lead to code duplication (i.e., useless efforts) and to inconsistencies if the implementations are not perfectly compatible. Instead, data hiding means that to access private data everybody MUST use the public methods that are provided, so that they are the same for everybody.

So encapsulation is required for data hiding to make sense, and at the same time it is helped by data hiding. Together they work well, but they are not the same thing!

Back to your question: in light of this, definition 1 is wrong. And 2, as noted by CommuSoft, isn't really a definition, it's a rule of thumb. And I will add that it's a rule of thumb about when to use data hiding, not encapsulation.

On a side note, electrometro suggests this might be a duplicate of this question. I think it is noteworthy to say that most answers there are wrong, including the top answer, which provides an example of encapsulation that actually is the contrary of encapsulation.

If you want external references, here are two articles about this:

Encapsulation is not information hiding

Abstraction, Encapsulation, and Information Hiding (please note that when he starts a paragraph called "ENCAPSULATION" and quotes a lot of definitions, he's just trying to show the confusion surrounding this topic; those definitions are wrong, as he explains later!)

8

Hiding data is about control. When you hide data, you hide it from other programmers (your future self included), because they might do something incorrect with the data, because they don't know as much about it as you do. So you want to carefully control how they use the data - in this case, by hiding it, by restricting access and visibility.

Encapsulation isn't really the practice of "protecting everything that is prone to change". Articles that claim this are overgeneralizing, because most software development techniques are about "protecting" things we might change later. What you should know is that it's often a good idea to examine things that are prone to change, and decide what kind of constraints you want to put on how other people see and use those things - how you might encapsulate those things.


...Similarly, encapsulation also isn't really the practice of "putting functions together with data", as a few articles linked in another answer claim. The proper term for that might be modularization, or plain old object oriented programming - there are many related concepts. These techniques can bring about encapsulation, but calling them encapsulation is confusing, especially for beginners. Encapsulation really is about "hiding" data. (To take a slightly advanced example, in JavaScript, you can put a value together with a function either as a property of the function object, or through a closure. Neither of these use fields, they don't involve using methods of a class/object/prototype, both put the data and function "together", but only the closure will encapsulate the data, by imposing access constraints.)

mk.
  • 11,360
  • 6
  • 40
  • 54
1

Encapsulation is data hiding, indeed. The reason is to group similar functionality that might change in the future in much more manageable manner.

Encapsulation might be achieved by much more than just using visibility modifiers.

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
1

I would go with the second one as the definition of encapsulation. I see it the same way. Encapsulation is a mechanism of protecting the rest of your code from changes.

The first statement is more restrictive. With encapsulation you don't hide just data, but also the implementation. And the visibility modifiers are just means of obtaining encapsulation. You use public for methods that are accessed from other classes. Changing these affects the outside code that calls them. While private hides the things you don't want to be visible to the outside and which can be freely chanhed without impact to other classes.

So the second statement is a definition of encapsulation, while the first one is a hint on how to obtain it.

Andrei Vajna II
  • 4,642
  • 5
  • 35
  • 38
1

Quoting Wikipedia:

"Encapsulation is the packing of data and functions into a single component. The features of encapsulation are supported using classes in most object-oriented programming languages, although other alternatives also exist.

In programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

  1. A language mechanism for restricting access to some of the object's components.

  2. A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data."

Jadiel de Armas
  • 8,405
  • 7
  • 46
  • 62