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:

    As information technology restructures the work situation, it abstracts thought from action.
    Shoshana Zuboff (b. 1951)

    She had never known before how much the country meant to her. The chirping of the insects in the long grass had been like the sweetest music. She had felt as if her heart were hiding down there, somewhere, with the quail and the plover and all the little wild things that crooned or buzzed in the sun. Under the long shaggy ridges, she felt the future stirring.
    Willa Cather (1873–1947)

    A mechanism of some kind stands between us and almost every act of our lives.
    Sarah Patton Boyle, U.S. civil rights activist and author. The Desegregated Heart, part 3, ch. 2 (1962)