next up previous contents
Next: subobjects Up: Using Classes Previous: subclasses

superclasses

As mentioned above classes are hierarchically organised generic descriptions. If a class has subclasses then it is automatically a superclass. Because all device server classes belong to the root class DevServerClass they are also automatically subclasses and their implementation is as for any other subclasses. It would be wrong to treat all subclasses of the root class serve in the same way. Not all classes are supposed to be instantiated, some classes exist only as superclasses for other subclasses. The best example of this is the root class DevServerClass. There are other examples (e.g. the PowerSupplyClass). The philosophy behind these superclasses is different from classes which occur only as subclasses of other classes and never as superclasses.

The idea behind a superclass is to abstract out what is common to a number of subclasses and implement this in a single class. This has the advantage of having only a single source to maintain. It also enforces reusability of code. Superclasses can be thought of as abstract classes which serve as place holders for data and a single common source for code. They are essential for implementing classes i.e. hierarchically organised generic descriptions.

Experience with class programming has shown that it is not a good idea to have too many levels of hierarchy. Nesting classes too deeply (i.e. more than five superclasses) is difficult to follow and dissuades programmers from reusing existing superclasses. The ideal level of nesting is three or in rare cases four levels of class hierarchy. Keep class hierarchies simple. It is more efficient to opt for a flat class structure with many toplevel classes than to go for heavily nested classes. Reusing existing classes implies reusing them as objects rather than as superclasses i.e. as subobjects.



Andy Goetz
Tue Jan 28 13:58:13 MET 1997