2.2.1 Encapsulation

Previous Index Next


Although the combining of data and code to manipulate the data into one entity (an object) is similar to having a library in a procedural language, several major differences exist.

One of these differences is that in a good software object, none of it's attributes (data) can be accessed or manipulated directly. Only the object's methods (code routines) should access and manipulate it's attributes. This builds a protective layer of code that shields the data from incorrect or invalid manipulation. This shielding or information hiding is known as Encapsulation.

Encapsulation provides several major benefits as detailed below.

The black box

By wrapping up data and code into an object a developer is in effect building a black box for other parts of the system to use. Other developers who use the object don't have to know how an object works. All they have to know is what methods they can call on the object and what the results of those methods will be.

Easy bug Fixing

The code for manipulating an objects data is held only in one place in the system, namely in the object's methods. This means that a bug fix only has to be applied to the code in the method and no where else in the system.

Easy enhancements

Methods on a object can be fined tuned without breaking any other parts of the system. In fact all the data structures and algorithms in an object can be changed without affecting any other part of the system. This makes enhancement of the system very easy.

Reuse

A well designed object can be reused in other systems. This means that a object can be written and tested once and then used in multiple systems. Once an object is used in more then one system, any enhancements or bug fixes to the object will automatically take affect in the various systems.