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:
“But while ignorance can make you insensitive, familiarity can also numb. Entering the second half-century of an information age, our cumulative knowledge has changed the level of what appalls, what stuns, what shocks.”
—Anna Quindlen (b. 1952)
“By school age, many boys experience pressure to reveal inner feelings as humiliating. They think their mothers are saying to them, You must be hiding something shameful. And shucking clams is a snap compared to prying secrets out of a boy whos decided to clam up.”
—Ron Taffel (20th century)
“The law isnt justice. Its a very imperfect mechanism. If you press exactly the right buttons and are also lucky, justice may show up in the answer. A mechanism is all the law was ever intended to be.”
—Raymond Chandler (18881959)