I have read almost more than 100 links and explored all the questions on SO but :( still unable to understand
The difference between Data Hiding and Encapsulation
While reading this answer I read this line
data hiding is encapsulation, but not all encapsulation is data hiding
So After all a huge research, I found that
1) Data hiding is achieved by encapsulation OR it is a form of encapsulation(Am I Right)?
2) If yes, applying access specifier is data hiding (and Encapsulation too) But what is the mechanism which is only Encapsulation but Not Data Hiding ?

- 3,797
- 3
- 24
- 42
-
2From the wikipedia article on information hiding: "The term encapsulation is often used interchangeably with information hiding. Not all agree on the distinctions between the two though; one may think of information hiding as being the principle and encapsulation being the technique." - I doubt that you'll get a definitive answer... – Jon Skeet Jun 19 '13 at 11:24
-
See the discussion at http://programmers.stackexchange.com/questions/173547/what-is-the-difference-between-data-hiding-and-encapsulation – Rajesh J Advani Jun 19 '13 at 11:25
-
i think you are splitting hairs. data hiding and encapsulation are pretty much the same thing. – Oliver Watkins Jun 19 '13 at 11:25
-
@JonSkeet So simply we can say that Encapsulation is all in all data hiding and data hiding is all in all encapsulation – Despicable Jun 19 '13 at 11:27
-
1@Despicable: I think all I can say for sure is that different people will interpret the terms in different ways. – Jon Skeet Jun 19 '13 at 11:29
-
1@JonSkeet I need the opinion of the Author of "C# in depth"? What does he say? – Despicable Jun 19 '13 at 11:33
-
I say that I have no more important opinion on it than anyone else. I don't think I'd have chosen those terms to start with though, as they're often more to do with *implementation* hiding than *information* hiding. – Jon Skeet Jun 19 '13 at 11:35
-
[Abstraction VS Information Hiding VS Encapsulation](https://stackoverflow.com/questions/24626/abstraction-vs-information-hiding-vs-encapsulation) – jaco0646 Oct 10 '18 at 16:26
3 Answers
The short answer:
1) Data hiding can be achieved without encapsulation, an example of this would be a private constant in a class, and that constant is not returned by any 'getter'.
2) Applying an access modifier might be data hiding and encapsulation. You can achieve encapsulation but not data hiding, when you expose the data, but only to be modified by getters and setters.
And the long answer:
Data hiding and encapsulation are quite different things, but related concepts. Data hiding is about not leaking the implementation details any user of your class, while encapsulation is preventing unexpected changes on the data.
The best explanation I've found of this is in the book `Growing Object Oriented Systems Guided by Tests" (page 49)
What the authors say is that encapsulation is almost always a good thing, but data hiding can be in the wrong place, and they give the following example:
- Encapsulate the data structure for the cache in the Loader class
- Encapsulate the name of the application's log file in the PrivacyPolicy class
Both of the above sound sensible, until we put them from the point of view of data hiding
- Hide the data structure for the cache in the Loader class
- Hide the name of the application's log file in the PrivacyPolicy class
In the example of the cache, it makes sense to hide it. But regarding the application log file name it doesn't make sense to hide it.

- 28,839
- 5
- 58
- 88
-
+1 for `Data hiding is about not leaking the implementation details any user of your class, while encapsulation is preventing unexpected changes on the data.` – Despicable Jun 20 '13 at 04:05
-
Awesome answer , very clear.Now I just want to know 1 thing and then ill accept your answer.1) From the line `encapsulation is preventing unexpected changes on the data` it means by applying getter setters we can eaisly achieve the encapsulation.Right?(If you have any other example then please share as well).2) From the line `Data hiding is about not leaking the implementation details any user of your class` It means not only from the Encapsulation , but also from the abstraction we can achieve the data hiding? (Also if you have any other example of data hiding then please share) – Despicable Jun 20 '13 at 04:08
-
if **Data hiding is about not leaking the implementation details** then what is diffrence between data hiding and abstraction? Are they same? – Samra Dec 05 '13 at 09:44
Abstaction
Focuses on essential aspects and hides background/implementation details
Focuses on outside view of the object Example : Stack class [Abstaction focuses on the services provided by the class Push, Pop]
- Abstraction allows capturing the entire object behaviour no more no less.
- It focuses on what object can do
- It helps in identifying the classes based on the responsibility driven approach [Classifying the system as set of objects based on responsibility carried out by a class]
Encapsulation
- Bundeling state and behaviour of object into single unit
- Achieved by defining class [Identigying states and behaviours and putting these two things together into class]
- Enables keeping states and behaviour of objects together
- It does not hide implementation details its purpose is to identifying states and behaviours and keeping these things together
- It focuses on the inside view of the object while abstaction focuses on the outside view of the object
- Encapsulation helps implementing abstaction
Information Hiding
- Encapsulation must allow only essential services to be revealed, Information hiding a concept related to the Encapsulation is required to hide the implementation details of an object
- Information hiding and Encapsulation are not same
Encapsulation is the decision to identify which states and behaviours of objects are to be put together Whereas Information hiding is the decision on which of the encapsulated items to be revealed to the user and what not to be reaveled to the user

- 359
- 1
- 7
Encapsulation hide complexity. Like we are creating getter and setter..
But Data Hiding means hiding something and In java we can done using access modifiers i.e.

- 1
- 1

- 2,152
- 1
- 28
- 28