In fact, there are only two types of geometric shapes, ellipses (which are circular) and polygons (which have a certain number of sides). A circle is a type of ellipse that has only one focus. Triangles, rectangles, and pentagons are all types of polygons with varying numbers of sides. A square is a type of rectangle with all sides of the same length. This constitutes a perfect inheritance relationship.
In this example, Shape is the base class for Ellipse and Polygon (all classes inherit from it). The ellipse has an attribute foci, which indicates the number of foci the ellipse has. Circle inherits ellipse, so circle is a subclass of ellipse, and ellipse is the superclass of circle. Similarly, Triangle, Rectangle, and Pentagon are all subclasses of Polygon, and Polygon is their superclass. Finally, Square inherits from Rectangle.
It is best to use a diagram to explain this inheritance relationship, which is UML ( This is where the Unified Modeling Language comes in. One of the main uses of UML is to visually represent complex object relationships like inheritance. Figure 4-1 is a UML diagram explaining the relationship between Shape and its subclasses.
In UML, each box represents a class, described by the class name. The line segments at the top of Triangle, Rectangle and Pentagon come together and point to Shape, indicating that these classes are inherited from Shape. Likewise, the arrow pointing from Square to Rectangle illustrates the inheritance relationship between them.
If you are interested in learning UML, you can refer to the books written by the three founders of UML “UML User Guide (Second Edition)”.