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:

    Many more children observe attitudes, values and ways different from or in conflict with those of their families, social networks, and institutions. Yet today’s young people are no more mature or capable of handling the increased conflicting and often stimulating information they receive than were young people of the past, who received the information and had more adult control of and advice about the information they did receive.
    James P. Comer (20th century)

    Style is a fraud. I always felt the Greeks were hiding behind their columns.
    Willem De Kooning (b. 1904)

    I’ve never known a Philadelphian who wasn’t a downright “character;” possibly a defense mechanism resulting from the dullness of their native habitat.
    Anita Loos (1888–1981)