Inheritance Hierarchy for Shapes
publish date: 1969/12/31 00:00:00 UTC
A shape can be classified as either 2-D or 3-D. Design an inheritance hierarchy. Which arrangement correctly represents the hierarchy with at least five shape classes?
Correct Answer
Explanation
The correct hierarchy has Shape as the root superclass with two direct subclasses: 2DShape and 3DShape. 2DShape is then subclassed into at least Circle, Rectangle, and Triangle. 3DShape is subclassed into at least Sphere and Cube. This gives a minimum of seven classes (Shape, 2DShape, 3DShape, Circle, Rectangle, Triangle, Sphere/Cube) with a clean generalization hierarchy. Common attributes (e.g., colour, area()) go in Shape; 2D-specific attributes (e.g., perimeter()) in 2DShape; 3D-specific (e.g., volume()) in 3DShape.
Reference
Software Engineering, Ian Sommerville, 10th edition
