Java – the difference between “data hiding” and “encapsulation”

designjavaobject-oriented

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?

Best Answer

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.

Related Topic