Heuristics for Subclassing - Variance

Subclassing for variation occurs when you have two or more classes that seem to have many features in common, for which it is clear that some code sharing could occur, but it is not entirely clear which one should be the parent and which one the child.

An example, you can imagine that a component to control a Mouse might have a lot in common with a component that managed a touch pad, or a joy stick. But which one should be the parent, and which one the child?

Rather than simply selecting one at random, a better alternative is to factor out the common parts, and create a new parent class that is ancestor to all three. This would be replacing subclassing for variation with subclassing for specialization.

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