Encyclopedia > Encapsulation in object-oriented programming

  Article Content

Information hiding

Redirected from Encapsulation in object-oriented programming

DEFINITION 1:


In computer science, information hiding is leave out some details of implementation on purpose from the public; it reduces dependency, which means the implementation or structure of an the object (computer science) can be modified without affecting code using the object.

Information hiding is one of key techniques used in object-oriented programming, a programming style employing interaction among objects. Reducing dependency is the primary motivation of modulization, diving the program a piece of independent, interchanable modules.

After details are hidden, an unfied interface can be, as often, offered instead. The way of hiding details and providing common interfaces is called encapsulation, which is an analogy from making an object looks like being covered by a capsule (interface in this case). This makes possible two objects differing in internal representation but having the common interface can be used interchangably (called interchangeability).

In many OOP languages, access-control is used to block some methods to be exposed to the public. Private modifer is state the method cannot be accessed by anyone but fellow methods in the same class.

For example, if the state of a point is represented internally with Cartesian coordinates then this might be changed to Polar coordinates without changing the interface of the object, i.e, without changing which messages with which arguments are understood by the object and what the type of the result of these messages will be.


DEFINITION 2:

In computer science and object-oriented programming, encapsulation refers to how objects contain and manipulate data. Encapsulation (also referred to as, "information hiding") is the practice of hiding the data structures which represent the internal state of an object from access by anything other than the publicly-exposed methods of that object. This ensures that objects cannot change the internal state of other objects in unexpected ways, minimizing the complexity of putting together modules of code from different sources.

Encapsulated code can generally be rewritten without any need to rewrite the encapsulating code.

The two main advantages of encapsulation are:

The data structure that represents the state can be reimplemented without affecting the implementation of the other objects. For example if the state of a point is represented internally with Cartesian coordinates then this might be changed to Polar coordinates without changing the interface of the object, i.e, without changing which messages with which arguments are understood by the object and what the type of the result of these messages will be. The state of the object can be guarded by the methods. The data structure that represents the state of the object may allow certain values that are not considered meaningful, e.g., a percentage may be represented by an integer that allows numbers larger than 100. The methods that update this integer can then ensure that it never rises above 100. Many languages allow the programmer to bypass encapsulation, or to specify varying degrees of encapsulation for specific object types. In Java, for example, a class might be defined like this:

  class Cow extends Mammal {
    public Tail m_tail;
    private Horns m_horns;

    
    public void eat(Food f) {
      super.eat(f);
      chewcud();
    }
    private void chewcud() {
      . . .
    }
  }

With the Cow type as defined above, some piece of code external to Cow's implementation would be allowed to access m_tail directly, without going through any of the methods that Cow has chosen to expose as its interface. Such code would not, however, be able to access m_horns. Likewise, the method eat() is exposed as part of the Cow's interface, so any other object would be able to cause the cow object to eat by calling that method, passing it the offered food item as an argument to the method. External code would not be able to directly cause the cow to call its chewcud() method (nor would it even know such a method existed; only the cow itself knows or cares that eating involves chewing its cud).



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Foxton Locks

... of ten Canal locks located on the Grand Union Canal about five miles west of the Leicestershire town of Market Harborough and are named after the nearby village of ...