Encapsulation (computer programming)

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.

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.

[citation needed] Some languages like Smalltalk and Ruby only allow access via object methods, but most others (e.g., C++, C#, Delphi or Java[12]) offer the programmer some control over what is hidden, typically via keywords like public and private.

Information hiding is accomplished by furnishing a compiled version of the source code that is interfaced via a header file.

Systems that provide object-level capability-based security (adhering to the object-capability model) are an exception, and guarantee strong encapsulation.

[13] Clients call the API functions to allocate, operate on, and deallocate objects of an opaque data type.

The source code for these functions defines the actual contents of the structure: Below is an example of Python, which does not support variable access restrictions.