[   ^ to index...   |   next -->   ]

CSE 341 : 31 May 2001

Static object-oriented typechecking

The cardinal principle of object-oriented subtyping:

A type SubA may only be a subtype of A if every instance of SubA can be safely substituted for any instance of A.

More specifically, we can say that, given a reference of type A which points to an object O:

It is not always possible to prove statically (at compile time) that one object will be substitutable for another at run time. Various OO type systems use different typing rules to approximate the "substitutability principle" statically. We'll first discuss the "natural" way of thinking about subclassing, and then we'll discuss the type system used in Java and C++.

An example

    class Fruit { ... }
    class Apple extends Fruit { ... }
    class Orange extends Fruit { ... }

    class FruitPlant
    {   Fruit produce() { ... }
    }
    class ApplePlant
    {   Apple produce() { ... }
    }

    class FruitFly
    {   void eat(Fruit f) { ... }
    }
    class AppleFly
    {   void eat(Apple a) { ... }
    }

Given these method profiles, what should the subclassing relationships be? Consider: will an ApplePlant ever produce something that a FruitPlant could not? What about vice versa? Also, can an AppleEatingFly eat() something that cannot be eaten by a FruitEatingFly? What about vice versa?


Keunwoo Lee
Last modified: Wed May 30 20:58:53 PDT 2001