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:
“Phenomenal nature shadows him wherever he goes. Clouds in the staring sky transmit to one another, by means of slow signs, incredibly detailed information regarding him. His inmost thoughts are discussed at nightfall, in manual alphabet, by darkly gesticulating trees. Pebbles or stains or sunflecks form patterns representing in some awful way messages which he must intercept. Everything is a cipher and of everything he is the theme.”
—Vladimir Nabokov (18991977)
“I am a man, and God is hiding from us humans. We are unable to see Him, we can only search for Him.”
—Friedrich Dürrenmatt (19211990)
“Ive never known a Philadelphian who wasnt a downright character; possibly a defense mechanism resulting from the dullness of their native habitat.”
—Anita Loos (18881981)