
As the diagram above shows, Encapsulation hides the implementation away from the user (in this case a lion) who can do all its operations without seeing how it does it. Also think of a car, we now how to get from destination A to B without knowing how the engine works under the bonnet or seeing it work.
Each class has two parts:
- An Interface
- An Implementation.
The Interface is the implementation code for the external Interface i.e. in the diagram the body of the lion.
The Implementation is the code that does all the operations, makes the interface look as if it is doing something. (All the functions the lion does such as sleep, eat, hunt etc..)
No Part of a complex system should depend on the internal details of another part.
Encapsulation is a technique for minimizing interdependencies among modules by defining a strict external interface. This way, internal coding can be changed without affecting the interface, so long as the new implementation supports the same (or upwards compatible) external interface.
Encapsulation prevents a program from becoming so interdependent that a small change has massive ripple effects.
The implementation of an object can be changed without affecting the application that uses it for:
Improving performance, fix a bug, consolidate code or for porting.
| << Class | Inheritance >> |