= Encapsulation
{disambiguate=computer programming}
{wiki}
<Encapsulation (computer programming)> bundles related program state and operations or hides implementation details behind an interface. <Encapsulation in object-oriented programming> realizes this through objects and their permitted operations; encapsulation also applies to modules outside object-oriented programs.
The useful boundary is a promise: clients request operations through an <API>, and the implementation maintains a <representation invariant>. Instead of proving that every client updates several storage fields consistently, prove that the permitted operations preserve the <representation invariant>. <Information hiding> also leaves room to replace the <data structure> while keeping the client-facing behavior, a property called <representation independence>.
Bundling state and operations alone does not establish that promise. <Representation exposure> can let clients bypass the <API>. A `private` annotation alone is not a security guarantee: it does not establish correctness of operations, isolation from hostile code or safe handling of shared mutable storage. The examples below concern sequential calls with no concurrent access and no client access to internal storage; they do not claim protection against arbitrary memory modification.
Back to article page