Encapsulation
Encapsulation is a design pattern that is supported by object-oriented programming languages. Encapsulation bundles data and methods that act on that data into a single unit of code – called a class.
Encapsulation is also known as information hiding, a term coined by David Parnas for the broader principle of shielding a module’s internal decisions from its callers, and is often described as hiding implementation details. The three names point at the same idea: a module exposes an interface and conceals everything behind it, so that callers depend only on what the interface promises and not on how it is built. When that boundary fails, the result is a leaky abstraction.
The purpose of encapsulation is to protect data from outside interference and misuse, and to free the implementation to change without forcing every caller to change with it — the discipline that encapsulating what changes turns into a design rule. The uniform access principle applies the same idea to one specific decision: whether a value is stored as an attribute or computed by a method. The Law of Demeter is a complementary discipline: it stops a caller from reaching through an object to the internals of that object’s collaborators, which would bypass the very boundary encapsulation draws. It is the foundation that makes loose coupling and composition practical, since both rely on callers depending on abstractions rather than on internals.
By hiding the meaning of values behind a named interface, encapsulation also weakens connascence, specifically connascence of meaning, where callers would otherwise have to agree on the interpretation of a raw value.