Difference between extending types and classesTopLaundry one more time

Laundry one more time

We've done laundry a few times already. Just be glad we didn't have pants and shirts falling from our clouds in the previous example.

When we modified our original laundry sorter to also handle pants, we included a Laundry type so that one variable could hold either Tshirts or Pants. However, there was a still lot of overlap between the code for Tshirts and Pants. Using inheritance and lists, we can "factor out" that common code into a superclass called AbstractLaundry. See the new laundry code, with the laundry items implemented in AbstractLaundry, Tshirt, and Pants
Recall our program used the Laundry type. Actual method bodies for the methods declared there appear in both Tshirt and Pants. However the code for the corresponding methods in each class are essentially the same. The only difference is the names and types of instance variables that correspond to the different pieces of a Tshirt (e.g., sleeves, neck, and body) or Pants (legs and waist).

Instead of having all those instance variables, we will hold the different components of each of the different kinds of laundry items in a pair of lists, one for solid items (which can change color) and the other for outlined items (which do not). The type of these lists must be compatible with framedRects, filledRects, framedOvals, and filledOvals. There might be other classes where we might also want to add text (e.g., for logos) or lines. Thus we will use Graphic, because it has all of the operations we need to use with laundry items. (We could also have used Graphic2D, for example, as the different kinds of rects and ovals implement this interface as well, though it wouldn't have worked with lines or text.) See the on-line code above.

The definition of solid and outline are both lists of Graphic. Both are created in the abstract superclass abstractLaundry, but no elements are added directly. Instead confidential method addSolid and addOutline are given to provide that capability to the subclasses. Because they are confidential they are not accessible outside the class or its subclasses (the classes that inherit from it).

The other methods of the abstract superclass use the array to iterate through the components, performing appropriate actions.

The methods move, moveTo, contains, removeFromCanvas, color:=, and color, etc. are all public because they implement the methods in the Laundry type. The methods addSolid and addOutline are confidential because they are to be called only from classes that inherit from abstractLaundry.

Once we have abstractLaundry, we can create objects and use addOutline and addSolid directly to build custom laundry items. We can simplify our Tshirt and Pants classes by having them inherit from abstractLaundry. We need only initialization code, as the methods of abstractLaundry were designed carefully to work with all kinds of combinations of graphic elements. See the code for Tshirt and Pants. With inheritance, it becomes much simpler for us to extend this program with more subclasses representing additional laundry items.


Difference between extending types and classesTopLaundry one more time