Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>The short answer:</h2> <p>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'.</p> <p>2) Applying an access modifier <strong>might</strong> 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.</p> <h2>And the long answer:</h2> <p>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.</p> <p>The best explanation I've found of this is in the book `Growing Object Oriented Systems Guided by Tests" (page 49)</p> <p>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:</p> <ul> <li>Encapsulate the data structure for the cache in the Loader class</li> <li>Encapsulate the name of the application's log file in the PrivacyPolicy class</li> </ul> <p>Both of the above sound sensible, until we put them from the point of view of data hiding</p> <ul> <li>Hide the data structure for the cache in the Loader class</li> <li>Hide the name of the application's log file in the PrivacyPolicy class</li> </ul> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload