User Guide
Example: GeometricShapes 157
Say that you wanted to add a new shape, a pentagon, to this example application. You would
create a new Pentagon class that extends the RegularPolygon class and defines its own versions
of the
getArea() and describe() methods. Then you would add a new Pentagon option to
the combo box in the application’s user interface. But that’s it. The Pentagon class would
automatically get the functionality of the
getPerimeter() method and the
getSumOfAngles() method from the RegularPolygon class by inheritance. Because it inherits
from a class that implements the IGeometricShape interface, a Pentagon instance can be
treated as an IGeometricShape instance too. That means you do not need to change any of the
methods in the GeometricShapeFactory class, making it much easier to add new types of
shapes when needed.
You may want to add a Pentagon class to the Geometric Shapes example as an exercise, to see
how interfaces and inheritance can ease the workload of adding new features to an
application.