Reputation: 3957
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 ?
Upvotes: 4
Views: 4246
Reputation: 2192
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.
Upvotes: 0
Reputation: 357
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]
Encapsulation
Information Hiding
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
Upvotes: 0
Reputation: 29877
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.
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:
Both of the above sound sensible, until we put them from the point of view of data hiding
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.
Upvotes: 4