We will design a hierarchy of classes to represent various shapes (including rectangles).
-
We define a parent class for movable point objects. Each instance of this class is represented by an x,y location, by a dx,dy offset for the object to move, and a color for the object. This example also illustrates data encapsulation, where users of the class must use methods to get or set data of the class.
-
A child class will represent rectangles by adding variables for width and height. It will also override the paint method to draw rectangles.
-
Other shapes are easily constructed as children of the rectangle class by overriding the paint method to draw a new shape.
|