Subclasses and Subtypes

Now let us return to the distinction between subclass and subtype we introduced in an earlier chapter.

To say that a class is a subclass of another is to simply assert a fact about how it was constructed, that is, the new object was constructed using inheritance from the old.

To say that a class is a subtype of another class is to make an assertion about behavior. It says that the behavior of the new thing includes all of the behavior of the old thing, and that the new thing can be used in a situation where I expect to see an instance of the old thing.

Not all subclasses are subtypes, nor are all subtypes subclasses.

The confusion arizes because in some languages, such as C++, subtypes can ONLY be formed out of subclasses. However, in other languages, such as Smalltalk, the ideas are totally independent.

Java is somewhat inbetween. In Java subtypes can be formed using subclasses, or they can be formed using interfaces.

[audio] [real] Text to accompany slide7, in Chapter 10 of An Introduction to Object-Oriented Programming