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

I read almost over 100 links and studied all the questions about SO, but: (still can not 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 extensive research, I found that 1 Data hiding is achieved by encapsulation OR is it a form of encapsulation (Am i Right)?
2) If so, using an access specifier is hiding data (and encapsulating it too). But what is the mechanism that is only encapsulation, but not data hiding?

+4
source share
3 answers

Short answer:

1) Data hiding can be achieved without encapsulation, an example of this is a private constant in a class, and this constant is not returned by any "getter".

2) Using an access modifier can hide data and encapsulation. You can achieve encapsulation, but not hide data when exposing data, but only to modify them using getters and setters.

And the long answer:

Hiding and encapsulating data are completely different things, but related concepts. Hiding data is not a leak of implementation information to any user of your class, while encapsulation prevents unexpected data changes.

The best explanation I found from this is in the book Growing Object-Oriented, Test-Driven Systems (p. 49)

What the authors say is that encapsulation is almost always good, but hiding data may not be in the right place, and they give the following example:

  • Encapsulate the data structure for the cache in the Loader class
  • Encapsulate the application log file name in the PrivacyPolicy class

Both of these sounds sound reasonable until we put them in terms of data hiding.

  • Hide cache data structure in Loader class
  • Hide application log file name in PrivacyPolicy class

In the cache example, it makes sense to hide it. But with regard to the name of the application log file, it makes no sense to hide it.

+4
source

Abstaction

  • Focuses on key aspects and hides background / implementation details

  • Focuses on the appearance of an object Example: Stack class [Abstaction focuses on the services provided by the Push, Pop class]

  • Abstraction allows you to capture the behavior of the entire object no more than no less.
  • It focuses on what the object can do.
  • This helps identify classes based on a responsibility-based approach [Classification of a system as a set of objects based on responsibility performed by a class]

Encapsulation

  • Masonry state and object behavior in a single block
  • Achieved by class definition [Identification of states and behavior and the unification of these two things into a class]
  • Allows you to save the state and behavior of objects together.
  • It does not hide implementation details, its purpose is to identify states and behavior and preserve these things.
  • It focuses on the internal representation of the object, while abstraction focuses on the appearance of the object.
  • Encapsulation helps implement abstaction

Information hiding

  • Encapsulation should allow disclosing only basic services. Information hiding the concept associated with encapsulation is required to hide the details of the implementation of the object
  • Information hiding and encapsulation do not match.

Encapsulation is the decision to determine which states and behavior of objects should be collected. While the information hides the decision about which of the encapsulated positions should be disclosed to the user and what did not need to be returned to the user

0
source

Encapsulation hides complexity . How we create a getter and setter.

But Data Hiding means hiding and in java we can do this with access modifiers, i.e.

0
source

Source: https://habr.com/ru/post/1487025/


All Articles