next up previous contents
Next: Inheritance Example Up: An Object-Oriented World Previous: An Object-Oriented World   Contents

Encapsulation Example

If we have decided to use a car for journey to Trieste, consider the steering mechanism of a car as an example of encapsulation.

Since a car was invented, the steering mechanism has not changed functionality. It presents the same interface to the users: everybody knows how to use this mechanism through its interface: if we turn the steering wheel clockwise, the car will turn to the right, if we turn it counterclockwise, our car will turn to the left. Therefore in object-oriented paradigm this mechanism is an object. We can use it without having any idea about implementation -- everything is hidden for us under the hood, only a few of us have more details: they even know that implementation of this mechanism is different for cars from different firms. The interface is naturally standard: in case of changing the mechanism by means of rental trailer we have some difficulties in using.

In turn, the steering mechanism object contains a lot of other more detailed embedded objects, each of which has it is own state, behaviour, interface and implementation. Those interfaces are not exposed to a driver, but to the other parts of the steering mechanism that use them.

A common approach in OOP is to hide the implementation and expose the interface through encapsulation.


next up previous contents
Next: Inheritance Example Up: An Object-Oriented World Previous: An Object-Oriented World   Contents
Olexiy Ye. Tykhomyrov 2001-12-16