Subtyping

You're looking at your class hierarchy and trying to decide if you have a good structure. There are three tests you can use to decide if one class should subclass from another or not. The first one is convenience. Is it convenient to be able to subclass from it because it has so many methods you can use and if you just override a few methods, maybe the new subclass will work properly. This isn't a very good test but it is a possibility. The second test is the Is-a test. Can you say that the subclass is a kind of superclass? For example, a car is a vehicle. This is a better test and, in fact, one that people often use but it's still not a perfect test. The best test I've seen is called the Liskov Substitution Principle or the Sybtyping test. Is the subtype interoperable with respect to the super type? Can I use an instance of the subclass anywhere I'd use an instance of the superclass? If you can do this, you probably have a good class hierarchy.

Download