Encapsulation (computer programming) explained

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. 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,[1] [2] 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.

Meaning

In object-oriented programming languages, and other related fields, encapsulation refers to one of two related but distinct notions, and sometimes to the combination thereof:[3] [4]

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 some programming languages that provide lexical closures view encapsulation as a feature of the language orthogonal to object orientation.

The second definition reflects that in many object-oriented languages, and other related fields, the components are not hidden automatically and this can be overridden. Thus, information hiding is defined as a separate notion by those who prefer the second definition.

The features of encapsulation are supported using classes in most object-oriented languages, although other alternatives also exist.

Encapsulation and inheritance

The authors of Design Patterns discuss the tension between inheritance and encapsulation at length and state that in their experience, designers overuse inheritance. They claim that inheritance often breaks encapsulation, given that inheritance exposes a subclass to the details of its parent's implementation.[9] As described by the yo-yo problem, overuse of inheritance and therefore encapsulation, can become too complicated and hard to debug.

Information hiding

See main article: Information hiding. Under the definition that encapsulation "can be used to hide data members and member functions", the internal representation of an object is generally hidden outside of the object's definition. Typically, only the object's own methods can directly inspect or manipulate its fields. Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state. A supposed benefit of encapsulation is that it can reduce system complexity, and thus increase robustness, by allowing the developer to limit the interdependencies between software components.

Some languages like Smalltalk and Ruby only allow access via object methods, but most others (e.g., C++, C#, Delphi or Java) offer the programmer some control over what is hidden, typically via keywords like public and private.[6] ISO C++ standard refers to protected, private and public as "access specifiers" and that they do not "hide any information". Information hiding is accomplished by furnishing a compiled version of the source code that is interfaced via a header file.

Almost always, there is a way to override such protection  - usually via reflection API (Ruby, Java, C#, etc.), sometimes by mechanism like name mangling (Python), or special keyword usage like friend in C++. Systems that provide object-level capability-based security (adhering to the object-capability model) are an exception, and guarantee strong encapsulation.

Examples

Restricting data fields

Languages like C++, C#, Java, PHP, Swift, and Delphi offer ways to restrict access to data fields.

Below is an example in C# that shows how access to a data field can be restricted through the use of a private keyword:class Program

Below is an example in Java:

public class Employee

Encapsulation is also possible in non-object-oriented languages. In C, for example, a structure can be declared in the public API via the header file for a set of functions that operate on an item of data containing data members that are not accessible to clients of the API with the extern keyword.[10] // Header file "api.h"

struct Entity; // Opaque structure with hidden members

// API functions that operate on 'Entity' objectsextern struct Entity * open_entity(int id);extern int process_entity(struct Entity *info);extern void close_entity(struct Entity *info);// extern keywords here are redundant, but don't hurt.// extern defines functions that can be called outside the current file, the default behavior even without the keyword

Clients call the API functions to allocate, operate on, and deallocate objects of an opaque data type. The contents of this type are known and accessible only to the implementation of the API functions; clients cannot directly access its contents. The source code for these functions defines the actual contents of the structure:// Implementation file "api.c"

  1. include "api.h"

struct Entity ;

// API function implementationsstruct Entity * open_entity(int id)

int process_entity(struct Entity *info)

void close_entity(struct Entity *info)

Name mangling

See main article: Name mangling. Below is an example of Python, which does not support variable access restrictions. However, the convention is that a variable whose name is prefixed by an underscore should be considered private.[11]

class Car: def __init__(self) -> None: self._maxspeed = 200 def drive(self) -> None: print(f"Maximum speed is .") redcar = Carredcar.drive # This will print 'Maximum speed is 200.'

redcar._maxspeed = 10redcar.drive # This will print 'Maximum speed is 10.'

See also

References

Notes and References

  1. Web site: What is Object-Oriented Programming (OOP)? . 2024-03-02 . App Architecture . en.
  2. Web site: Encapsulation in Object Oriented Programming (OOPS) . 2024-03-02 . www.enjoyalgorithms.com.
  3. Book: Scott, Michael Lee . Programming language pragmatics . 2 . Morgan Kaufmann . 2006 . 978-0-12-633951-2 . 481 . Encapsulation mechanisms enable the programmer to group data and the subroutines that operate on them together in one place, and to hide irrelevant details from the users of an abstraction.
  4. Book: Nell B. . Dale . Chip . Weems . Programming and problem solving with Java . 2nd . Jones & Bartlett . 2007 . 978-0-7637-3402-2 . 396.
  5. Book: Mitchell, John C. . John C. Mitchell . Concepts in programming languages . Cambridge University Press . 2003 . 978-0-521-78098-8 . 522.
  6. Book: Pierce, Benjamin . Benjamin C. Pierce . Types and Programming Languages . MIT Press . 2002 . 978-0-262-16209-8 . 266 . Types and Programming Languages .
  7. Web site: Rogers . Wm. Paul . 2001-05-18 . dmy . Encapsulation is not information hiding . . 2020-07-20.
  8. Book: Thomas M. . Connolly . Carolyn E. . Begg . Database systems: a practical approach to design, implementation, and management . 4th . Pearson Education . 2005 . 978-0-321-21025-8 . Ch. 25: Introduction to Object DMBS ยง Object-oriented concepts . 814.
  9. Book: Gamma. Erich. Helm. Richard. Johnson. Ralph. Vlissides. John. Design Patterns. 1994. Addison-Wesley. 978-0-201-63361-0. registration.
  10. Book: King . K. N. . 2008 . W. W. Norton & Company . 978-0393979503 . 2nd . 464.
  11. Web site: Bader . Dan . The Meaning of Underscores in Python . Improve Your Python Skills . 1 November 2019.