Despicable
Despicable

Reputation: 3957

data hiding is encapsulation, but not all encapsulation is data hiding

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

Answers (3)

sharma_kunal
sharma_kunal

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

Amar T
Amar T

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]

  • 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

Upvotes: 0

Augusto
Augusto

Reputation: 29877

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.

Upvotes: 4

Related Questions