Polymorphism 473
14
Mixins and Capabilities Classes ....................................................................
One way to strike a middle ground between multiple inheritance and single inheritance is
to use what are called mixins. Thus, you might have your Horseclass derive from
Animaland from Displayable. Displayablewould just add a few methods for display-
ing any object onscreen.
A mixin, or capability class, is a class that adds specialized functionality without adding
many additional methods or much data.
Capability classes are mixed into a derived class the same as any other class might be, by
declaring the derived class to inherit publicly from them. The only difference between a
capability class and any other class is that the capability class has little or no data. This is
an arbitrary distinction, of course, and is just a shorthand way of noting that at times all
you want to do is mix in some additional capabilities without complicating the derived
class.
This will, for some debuggers, make it easier to work with mixins than with more com-
plex multiply inherited objects. In addition, less likelihood exists of ambiguity in access-
ing the data in the other principal base class.
For example, if Horsederives from Animaland from Displayable,Displayablewould
have no data. Animalwould be just as it always was, so all the data in Horsewould
derive from Animal, but the functions in Horsewould derive from both.
The term mixincomes from an ice cream store in Sommerville,
Massachusetts, where candies and cakes were mixed into the basic ice cream
flavors. This seemed like a good metaphor to some of the object-oriented
programmers who used to take a summer break there, especially while
working with the object-oriented programming language SCOOPS.
NOTE
Abstract Data Types ............................................................................................
Often, you will create a hierarchy of classes together. For example, you might create a
Shapeclass, and derive from that Rectangleand Circle. From Rectangle, you might
derive Squareas a special case of Rectangle.
Each of the derived classes will override the Draw()method, the GetArea()method, and
so forth. Listing 14.7 illustrates a bare-bones implementation of the Shapeclass and its
derived Circleand Rectangleclasses.