next up previous contents
Next: SELF_TYPE Up: The Cool Reference Manual1 Previous: Inheritance   Contents

Types

In Cool, every class name is also a type. In addition, there is a type SELF_TYPE that can be used in special circumstances.

A type declaration has the form x:C, where x is a variable and C is a type. Every variable must have a type declaration at the point it is introduced, whether that is in a let, case, or as the formal parameter of a method. The types of all attributes must also be declared.

The basic type rule in Cool is that if a method or variable expects a value of type P, then any value of type C may be used instead, provided that P is an ancestor of C in the class hierarchy. In other words, if C inherits from P, either directly or indirectly, then a C can be used wherever a P would suffice.

When an object of class C may be used in place of an object of class P, we say that C conforms to P or that $\tt C \leq P$ (think: C is lower down in the inheritance tree). As discussed above, conformance is defined in terms of the inheritance graph.

Definition 4.1 (Conformance)   Let $\tt A,C,$ and $\tt P$ be types.

Because Object is the root of the class hierarchy, it follows that $\tt A \leq Object$ for all types $\tt A$.



Subsections
next up previous contents
Next: SELF_TYPE Up: The Cool Reference Manual1 Previous: Inheritance   Contents