Encapsulation (computer programming)

In software systems, encapsulation refers to the bundling of data with the mechanisms or methods that operate on the data. It may also refer to the limiting of direct access to some of that data, such as an object's components.[1] Essentially, encapsulation prevents external code from being concerned with the internal workings of an object.

Encapsulation allows developers to present a consistent interface that is independent of its internal implementation. As one example, encapsulation can be used to hide the values or state of a structured data object inside a class. This prevents clients from directly accessing this information in a way that could expose hidden implementation details or violate state invariance maintained by the methods.

Encapsulation also encourages programmers to put all the code that is concerned with a certain set of data in the same class, which organizes it for easy comprehension by other programmers. Encapsulation is a technique that encourages decoupling.

All object-oriented programming (OOP) systems support encapsulation,[2][3] but encapsulation is not unique to OOP. Implementations of abstract data types, modules, and libraries also offer encapsulation. The similarity has been explained by programming language theorists in terms of existential types.[4]

  1. ^ Cite error: The named reference Rogers01 was invoked but never defined (see the help page).
  2. ^ "What is Object-Oriented Programming (OOP)?". App Architecture. Retrieved 2024-03-02.
  3. ^ "Encapsulation in Object Oriented Programming (OOPS)". www.enjoyalgorithms.com. Retrieved 2024-03-02.
  4. ^ Pierce 2002, § 24.2 Data Abstraction with Existentials

From Wikipedia, the free encyclopedia · View on Wikipedia

Developed by Tubidy