go to previous page   go to home page   go to next page


Not hard at all. Just one method needs to be changed — the access method that implements the rule. If balance was not marked private you would have to look over every line of the entire program to find places that changed it.


black box


The programming technique we have been talking about is called encapsulation.

Encapsulation means hiding the details of an object from the other parts of a program. The object can be used only through its access methods, which are carefully written to keep the object consistent and secure.

Encapsulation makes an object look like a black box: The insides of the box are hidden from view. Controls are on the outside of the box. The user can change the operation of the box only by using the controls.

The usual example of this is a TV set where the inner workings are sealed in a box. The user interacts with the set using some well-defined controls. The controls are sometimes called the user interface. In object oriented programming, programmers try to make the interface to the object simple and useful. The inner workings of the object should be made private.


(Thought question: ) Do you think that it is possible to have a private method?