I'm reading "Java concurrency in practice" and there is said: "Fortunately, the same object-oriented techniques that help you write well-organized, maintainable classes - such as encapsulation and data hiding -can also help you create thread-safe classes."

The problem #1 - I never heard about data hiding and don't know what it is.

The problem #2 - I always thought that encapsulation is using private vs public, and is actually the data hiding.

Can you please explain what data hiding is and how it differs from encapsulation?

  • 2
    Read Code Complete 2nd edition. It will answer many of your question. Commented Nov 5, 2012 at 18:26
  • Regarding information hiding, see: javaworld.com/jw-05-2001/jw-0518-encapsulation.html Commented Nov 5, 2012 at 22:55
  • 1
    This link has useful insights into this Information Hiding a design principle, specifies that a design decision should be hidden from the rest of the system to prevent unintended coupling. It should inform the way you encapsulate things, but of course it doesn't have to. Encapsulation is a programming language feature.
    – Abhijeet
    Commented Jun 29, 2016 at 3:07

7 Answers 7


Data and information hiding are a broader notions, found in computer science & software engineering. It refers to the fact that those part of a computer program that may change must not be accessible from other modules/from clients.

Encapsulation is a term that is found in Object-Oriented paradigm and refers to keeping the data in private fields and modify it only through methods.

Thus encapsulation may be seen as a way of achieving data hiding in object-oriented systems.

  • 1
    Thank you for reply. I still don't understand, why if data hiding is the same but just a bit broader than encapsulation, it's referred to in the book as if they were of similar broadness and not one including another.
    – dhblah
    Commented Nov 4, 2012 at 14:46
  • The authors probably make a difference from data hiding and information hiding in the sense that data hiding only hides the data structures - the private fields - while information hiding may refer to encapsulate implementation details (algorithms used for example) communication being done only through interfaces.
    – Random42
    Commented Nov 4, 2012 at 16:56
  • 2
    You can have one without having the other - look at Python, where there is no data hiding at all (no such thing as a private or protected attribute).
    – Latty
    Commented Nov 4, 2012 at 16:57
  • @Lattyware's point is very true and important. One can also have encapsulation (by your definition, which I object to because of what I'm describing here) without hiding anything, namely by blindly exposing a trivial getter and setter for every member. That doesn't hide anything and doesn't prevent anything, but fulfills the common definition of encapsulation to the letter.
    – user7043
    Commented Nov 4, 2012 at 17:20
  • 1
    Encapsulation is not keeping data in private field, that's more information hiding. Encapsulation, in a general way, is the concept of putting together (encapsulate) information/data and methods into a class.
    – user145653
    Commented Apr 11, 2015 at 22:15

Encapsulation and data hiding are related terms. It important to understand that they arise in relation to Abstraction. Booch et. al. in Object-Oriented Analysis and Design with Applications explains,

Abstraction and encapsulation are complementary concepts: Abstraction focuses on the observable behavior of an object, whereas encapsulation focuses on the implementation that gives rise to this behavior. Encapsulation is most often achieved through information hiding (not just data hiding), which is the process of hiding all the secrets of an object that do not contribute to its essential characteristics; typically, the structure of an object is hidden, as well as the implementation of its methods.


Creating a class includes the concept of encapsulation. When you create a class you put data and behaviors inside the class and the class becomes one unit that we call an object. So data hiding is part of encapsulation.


From Wikipedia:

In a programming language, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:

  • A language mechanism for restricting access to some of the object's components.
  • A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

Some programming language researchers and academics use the first meaning alone or in combination with the second as a distinguishing feature of object oriented programming, while other programming languages which provide lexical closures view encapsulation as a feature of the language orthogonal to object orientation.

The second definition is motivated by the fact that in many OOP languages hiding of components is not automatic or can be overridden; thus, information hiding is defined as a separate notion by those who prefer the second definition.

  • Thanks for your reply. A follow up questions: 1) does java programming language have facilities that second notion related to encapsulation refers to? 2) I don't understand why overriding of data hiding matters. E.g. in java you can access any field (public or private) through reflection.
    – dhblah
    Commented Nov 4, 2012 at 14:42
  • @Software Engeneering Learner: classes as source code constructs represent the second notion. As for information hiding, the point is that limiting the scope of data is often based on specific and separate language mechanisms. Commented Nov 5, 2012 at 8:11

They are often used interchangablely in discussion, and often I think they work together to achieve the same purpose, and while the following may not be completely accurate, it may provide some meaningful distinction, if a distinction needs to be made:

When talking about encapsulation, often it is implemented as a procedural/functional mechanism. There is some type of guard for the underlying state, and access through the guard requires certain protocols be followed to be given access (read or change the desired state). Encapsulation also offers the opportunity for side-effects to occur due to access (like cascading state change, or notification/raising an event/issuing a signal when the thing of interest is read or changed) so follow-up actions can be initiated. Again, I often think of encapsulation as a concept that is something that is implemented as a procedure.

I see the concept of data hiding is similar in purpose to encapsulation; however, the mechanism is structural and operates at a different level. In practice, instead of providing a guard and side-effect mechanism through procedure, state is protected and affected through structural mechanisms of the language and runtime. These types of guards would be visibility clauses, type definitions, inheritence, and the like. Side-effects that you can leverage from the structurally guarded objects are again something that depend on language and runtime: perhaps object activation, reference count, or something along those lines.


They are often, maybe usually, used interchangeably. But note the Booch quote above says, "Encapsulation is most often achieved through information hiding..." i.e. most often, but not always in every case.

Note that Python allows rolling data up in classes, but does not allow private variables. So it could be said that Python provides encapsulation without data hiding.

You could do the same thing in Java by making all your member variables public, but aside from giving everyone a heart attack, you'd loose the benefit of data hiding ... i.e. preserving the semantics of an object by restricting access to its state.


In O-O, Encapsulation is where information is held within an Object. For example, a Person has a name and clients of Person (i.e. you) know that Person holds a name, either via public fields or accessor methods. And, hopefully, you don't need to also hold the names in some global array of names, etc. So this is a big step to avoid unmanageable spaghetti code. But the client still needs to know something about how Person handles names: e.g. space or comma delimited?

Data Hiding is where Person has a name field, but, at least in theory, nobody knows. The field is private with no public accessor methods. Clients may pass a name from a database record, XML, HTTP POST, whatever, but the internal working of how Person handles the name are a "black box". Future implementations of Person are free to change, for example, to switch to having a firstName and a lastName.

In an ideal world Data Hiding is superior to Encapsulation, but not all worlds are ideal. :-)

Not the answer you're looking for? Browse other questions tagged or ask your own question.