Encapsulation (object-oriented Programming) - As Information Hiding Mechanism

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:

namespace Encapsulation { class Program { public class Account { private decimal accountBalance = 500.00m; public decimal CheckBalance { return accountBalance; } } static void Main { var myAccount = new Account; var myBalance = myAccount.CheckBalance; // This Main method can check the balance via the public // "CheckBalance" method provided by the "Account" class // but it cannot manipulate the value of "accountBalance" } } }

Read more about this topic:  Encapsulation (object-oriented Programming)

Famous quotes containing the words information, hiding and/or mechanism:

    The real, then, is that which, sooner or later, information and reasoning would finally result in, and which is therefore independent of the vagaries of me and you. Thus, the very origin of the conception of reality shows that this conception essentially involves the notion of a COMMUNITY, without definite limits, and capable of a definite increase of knowledge.
    Charles Sanders Peirce (1839–1914)

    When we retreat to the country, we are hiding not from people, but from our pride, which, in the city and among people, operates unfairly and immoderately.
    Anton Pavlovich Chekhov (1860–1904)

    The two elements the traveler first captures in the big city are extrahuman architecture and furious rhythm. Geometry and anguish. At first glance, the rhythm may be confused with gaiety, but when you look more closely at the mechanism of social life and the painful slavery of both men and machines, you see that it is nothing but a kind of typical, empty anguish that makes even crime and gangs forgivable means of escape.
    Federico García Lorca (1898–1936)