Download Navigating the MeldC - Academic Commons
Transcript
Chapter 2 Inheritance Our conception of object-oriented programming and how it is implemented in the MeldC language has so far covered issues of class, object creation, and message passing between those objects. We now come to an important variation on the idea of class in MeldC: the concept of inheritance. In MeldC, classes can be grouped into larger categories called superclasses, as in the animal world species can be grouped into ever-larger categories, from genus up to kingdom. When a class is grouped into a more encompassing class, it is said to inherit from its superclass. To view from a dierent angle, subclasses can be said to \reuse" information in existing superclasses to create something more specialized. There are as many ways to visualize inheritance as there are natural examples that deal with inheritance. One way to look at inheritance is the Venn diagram of Figure 2.1. In it, the class bagel inherits from classes torus and breakfast foods, and is shown as being inside the intersection of its two superclasses. The class seeded bagel is shown as inside the class bagel, which it inherits. Since seeded bagel is also inside the intersection of torus and breakfast foods, it is clear that it inherits from its \grandsuperclasses" as well. Venn diagrams tend to get complicated quickly, though, so in this manual, inheritance schemes will be illustrated by directed, acyclic graphs, like family trees (see Figure 2.2). It is important that the graph be acyclic, so as to avoid the case of an object inheriting from a class which inherits from 25