6.4.  Inheritance Design

[ fromfile: inheritance-intro.xml id: classdesign ]

Sometimes defining an inheritance relationship helps at first (e.g., by reducing redundant code) but causes problems later when other classes must be added to the hierarchy. Some up-front analysis can help make things easier and avoid problems later.

Example 6.14, in which we derive from the abstract Shape class, demonstrates an inheritance relationship with two levels of depth. The Rectangle class was used as a classification of objects and also as a concrete base class.

Is a square a kind of rectangle? Geometrically it certainly is. Here are some definitions borrowed from elementary geometry.

As you attempt to represent an inheritance tree of classes for an application that you are designing, it helps to list the kinds of capabilities that you need to provide for each class. For geometric shapes, they might be

After you describe the interface in further detail, you may find that the classic geometric definitions for shape classification are not the ideal taxonomy for shape classes within the context of your application.

As you perform an analysis some questions arise:

Using a UML modeling tool makes it easier to try out different ideas before writing concrete code. UML diagrams are especially useful for focusing on and describing small parts of a larger system. Figure 6.4 has concrete classes that serve as templates for creating the more "specific" shapes.

Figure 6.4.  Another Way to Represent Shapes

Another Way to Represent Shapes

In this tree diagram, the classes in leaf nodes are constrained versions of their base classes. The interface for the vector representations, drawing, and loading/saving of the objects is established in the abstract base classes.

Questions

  1. In the geometric sense, given a circle, you can prove it is also an ellipse, because an equation exists that specifies an ellipse, with its two foci being equal. In contrast, Figure 6.4 shows Ellipse to be a kind of Circle, with an extra point, or an extra degree of freedom. Would it make more sense to reverse the inheritance relationship? Or to have a completely different tree?

  2. Can you describe a better is-a relationship between two of these classes?

  3. Consider Figure 6.5, the Shape classes from the Qt GraphicsView library.[48]

    Figure 6.5.  QGraphicsItem Inheritance

    QGraphicsItem Inheritance

    Notice, there are no concrete Circle or Square items. Why do you think that is?

    Why do you think there is a Rectangle and a Polygon item?



[48] For more information about these classes, refer to the Qt Graphics View Framework Overview in the Qt Assistant.