Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Algorithmic Abstraction

Abstract classes can be used in many interesting ways. One of the most useful paradigms is the use of an abstract class for algorithmic abstraction . The erase and moveTo methods defined in Program gif are examples of this.

The erase and moveTo methods are implemented in the abstract class GraphicalObject. The algorithms implemented are designed to work in any concrete class derived from GraphicalObject, be it Circle, Rectangle or Square. In effect, we have written algorithms that work regardless of the actual class of the object. Therefore, such algorithms are called abstract algorithms.

Abstract algorithms typically invoke abstract methods. For example, both moveTo and erase ultimately invoke draw to do most of the actual work. In this case, the derived classes are expected to inherit the abstract algorithms moveTo and erase and to override the abstract method draw. Thus, the derived class customizes the behavior of the abstract algorithm by overriding the appropriate methods. The Java method resolution mechanism ensures that the ``correct'' method is always called.


next up previous contents index

Bruno Copyright © 1998 by Bruno R. Preiss, P.Eng. All rights reserved.