Information hiding
In computer science, information hiding is leaving out some details of implementation on purpose from the public. Encapsulation is, besides hiding, to provide common interfaces.The motivation of information hiding is to reduce dependency between client code and internal structure and its manipulation code. Client code can store and retrieve data via interfaces, or if not needed, no mechanism to access hidden code or date is provided. Accessing hidden data and code is usually designed to cause compile-time or run-time error. The implementation or structure can be modified without affecting code using the structure.
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.
Reducing dependency is the primary motivation for modularization, dividing the program into independent, interchangeable modules. Many modularization mechanisms such as namespaces and objects in object-oriented programming provide an access control for defining which code or data can be accessed externally or not.
After details are hidden, a unified interface can be offered instead. Hiding details and providing common interfaces is called encapsulation, which is an analogy from making an object look like it's covered by a capsule (the interface in this case). This allows two objects differing in internal representation but having a common interface to be interchangeably usable (called interchangeability). Interfaces also allow to facilitate the use of data structure and guard the state of the object from invalid input or modification of the structure. For instance, a percentage may be represented by an integer that allows numbers larger than 100. The code to 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).Data hiding implies that data is hidden from the user. In other words, the user only can "see" data that is necessary or relevant to the user, everything else remains hidden.