Solutions: Exercise 5

Inheritance

Remember: The solutions below are only suggested solutions.

The inheritance hierarchy might look like this:

An abstract class Graphical object, which defines the common properties of all the drawable, graphical objects in the editor. In this case, the attribute colour and the operations move, draw and remove.

Note that Points are not graphical objects, instead they are used to represent the positions of Lines, Circles and Rectangles.

What about the methods implementing the operations -- are they also shared by the subclasses of Graphical Object?

That is difficult to say before looking into the details of the methods. Maybe some methods will be identical for all three categories of graphical object, and thus defined at the top level in the hierarchy, while others will be defined in the subclasses.

For developers writing code using the classes above, it does not matter how the methods are implemented or in which class they are placed.

At the top level, all we need to know is that there are three common operations move, draw and remove available for all subclasses of Graphical object, so we can send these messages to all graphical objects.

The concept that an object may have relations to and communciate with other objects of "unknown" class is called polymorphism (greek: poly=many, morph=shape)


previous | next | start | contents | dictionary | help | evaluation

© Ericsson Telecom AB, 1995, Stockholm, Sweden