Simberon Design Minute
 

Inherit for Behavior

You need to implement a new class. You list out the instance variables you need and realize that most of these variables already exist in another class. Maybe you should create your new class as a subclass of the existing class. Before you go jumping to that decision, look at why you think this is a good subclass. You shoudn't create subclasses so that they can inherit structure - the instance variables. You create subclasses to inherit behavior. Do the operations defined in the superclass make sense and are applicable to the subclass? For example, suppose you want to implement a circle class. You may decide that a circle needs an x, a y and a radius. Points already have x and y coordinates so by subclassing from Point, you get those variables for free. The problem is that you get a lot of other baggage as well - convert to polar methods, point arithmetic, vector length calculations, distance from a line methods. Most of these methds don't make sense for a Circle. A circle should have a center and a radius. The center variable points to a point. In this way, you are saying that the circle has a point for the center instead of saying that a circle is a point.

Download