As Information Hiding Mechanism
Under this definition, encapsulation means that the internal representation of an object is generally hidden from view outside of the object's definition. Typically, only the object's own methods can directly inspect or manipulate its fields. Some languages like Smalltalk and Ruby only allow access via object methods, but most others (e.g. C++, C# or Java) offer the programmer a degree of control over what is hidden, typically via keywords like public and private. It should be noted that the ISO C++ standard refers to 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.
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 benefit of encapsulation is that it can reduce system complexity, and thus increases robustness, by allowing the developer to limit the interdependencies between software components.
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++.
Below is an example in C# that shows how access to a data field can be protected through the use of a private keyword:
Read more about this topic: Encapsulation (object-oriented Programming)
Famous quotes containing the words information, hiding and/or mechanism:
“I am the very pattern of a modern Major-Gineral,
Ive information vegetable, animal, and mineral;
I know the kings of England, and I quote the fights historical,
From Marathon to Waterloo, in order categorical;”
—Sir William Schwenck Gilbert (18361911)
“O hiding hair and dewy eyes,
I am no more with life and death,
My heart upon his warm heart lies,
My breath is mixed into his breath.”
—William Butler Yeats (18651939)
“When one of us dies of cancer, loses her mind, or commits suicide, we must not blame her for her inability to survive an ongoing political mechanism bent on the destruction of that human being. Sanity remains defined simply by the ability to cope with insane conditions.”
—Ana Castillo (b. 1953)