next up previous contents
Next: Types Up: Classes Previous: Features   Contents


Inheritance

If a class definition has the form

class C inherits P { ... };
then class C inherits the features of P. In this case P is the parent class of C and C is a child class of P.

The semantics of C inherits P is that C has all of the features defined in P in addition to its own features. In the case that a parent and child both define the same method name, then the definition given in the child class takes precedence. It is illegal to redefine attribute names. Furthermore, for type safety, it is necessary to place some restrictions on how methods may be redefined (see Section 6).

There is a distinguished class Object. If a class definition does not specify a parent class, then the class inherits from Object by default. A class may inherit only from a single class; this is aptly called ``single inheritance.''3 The parent-child relation on classes defines a graph. This graph may not contain cycles. For example, if C inherits from P, then P must not inherit from C. Furthermore, if C inherits from P, then P must have a class definition somewhere in the program. Because Cool has single inheritance, it follows that if both of these restrictions are satisfied, then the inheritance graph forms a tree with Object as the root.

In addition to Object, Cool has four other basic classes: Int, String, Bool, and IO. The basic classes are discussed in Section 8.


next up previous contents
Next: Types Up: Classes Previous: Features   Contents