10.2 Inheritance

Objects are particularly well-suited to programs that involve modeling real or imaginary worlds such as graphical user interfaces (modeling windows, files, and folders on a desktop), simulations (modeling physical objects in the real world and their interactions), and games (modeling creatures and things in an imagined world).

Objects in the real world (or most simulated worlds) are complex. Suppose we are implementing a game that simulates a typical university. It might include many different kinds of objects including places (which are stationary and may contain other objects), things, and people. There are many different kinds of people, such as students and professors. All objects in our game have a name and a location; some objects also have methods for talking and moving. We could define classes independently for all of the object types, but this would involve a lot of duplicate effort. It would also make it hard to add a new behavior to all of the objects in the game without modifying many different procedures.

The solution is to define more specialized kinds of objects using the definitions of other objects. For example, a student is a kind of person. A student has all the behaviors of a normal person, as well as some behaviors particular to a student such as choosing a major and graduating. To implement a student class, we want to reuse methods from the person class without needing to duplicate them in the student implementation. We call the more specialized class (in this case the student class) the subclass and say student is a subclass of person. The reused class is known as the superclass, so person is the superclass of student. A class can have many subclasses but only one superclass.1

Figure 10.2 illustrates some inheritance relationships for a university simulator. The arrows point from subclasses to their superclass. A class may be both a subclass to another class, and a superclass to a different class. For example, person is a subclass of movable-object, but a superclass of student and professor.

Figure 10.2: Inheritance hierarchy.

Our goal is to be able to reuse superclass methods in subclasses. When a method is invoked in a subclass, if the subclass does not provide a definition of the method, then the definition of the method in the superclass is used. This can continue up the superclass chain. For instance, student is a subclass of person, which is a subclass of movable-object, which is a subclass of sim-object (simulation object), which is the superclass of all classes in the simulator.

Hence, if the sim-object class defines a get-name method, when the get-name method is invoked on a student object, the implementation of get-name in the sim-object class will be used (as long as neither person nor movable-object defines its own get-name method).

When one class implementation uses the methods from another class we say the subclass inherits from the superclass. Inheritance is a powerful way to obtain many different objects with a small amount of code.

  1. Some object systems (such as the one provided by the C++ programming language) allow a class to have more than one superclass. This can be confusing, though. If a class has two superclasses and both define methods with the same name, it may be ambiguous which of the methods is used when it is invoked on an object of the subclass. In our object system, a class may have only one superclass.