
Encapsulation and information hiding
Encapsulation is one of the basic principles of the OOP paradigm. It allows us to bundle it into one object both data and functionalities that operate on that data.
Using the methods exposed by the object to manipulate the data, we take advantage of the encapsulation principle ignoring its internal complexity and implementation details. In other words, encapsulation hides the internal details regarding how the object manipulates its data. This feature, called the information hiding principle, is often related to encapsulation, although it is a more general principle. By hiding internal details, we obtain at least two great benefits:
- We provide a simple way to use an object, hiding the internal complexity
- We decouple the internal implementation from the use of the object, simplifying change management
The information hiding principle enforces the design of objects to have at least two parts: a public part and a private one. Only the public part is accessible by clients that want to interoperate with the object.
Many object-oriented languages, such as Java and C#, provide specific keywords such as public and private (access modifiers) to allow developers to easily implement the Information Hiding principle. JavaScript does not include such keywords. All members in an object are public by default. However, some common patterns can be used to obtain different levels of information hiding as happens in most classical OOP languages.