[ 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.
A shape is a closed two-dimensional object in the plane, with a graphical way of representing itself, together with a point that is considered its "center".
A rectangle is a shape consisting of four straight line segments with only 90-degree angles.
A square is a rectangle with equal sides.
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
Drawable
Scalable
Loadable
Savable
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:
What are the common operations you want to perform on all shapes?
What other kinds of shapes might you use in your application?
Why do you need a Rectangle
class as the base class of a Square
?
Can a Square
substitute for a Rectangle
?
A rhombus is four-sided, like a rectangle, so should
Rectangle
derive from Rhombus
?
Should you have a base class for all four-sided objects?
Should you have a different base class for all five-sided objects?
Should you have a general base class for polygons with the number of sides as an attribute?
Is your program going to perform geometric proof searches to identify objects?
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.
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.
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?
Can you describe a better is-a relationship between two of these classes?
Consider Figure 6.5, the Shape classes from the Qt GraphicsView library.[48]
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.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |