This installment of the Object-Oriented Thought Process completes the set of three articles dedicated to what I consider to be the four most important object-oriented concepts:
These are the concepts that must be mastered before we can explore more detailed object-oriented technologies. In future columns, we will focus more on object-oriented design techniques and implementation strategies. Because encapsulation was covered in the two previous articles, we will start here with the topic of inheritance.
Inheritance allows a class to inherit the attributes and methods of another class. This allows you to create brand new classes by abstracting out common attributes and behaviors.
One of the major design issues in OO programming is to factor out commonality of the various classes. For example, say you have a Dog class and a Cat class, and each will have an attribute for eye color. In a procedural model, the code for Dog and Cat would each contain this attribute. In an OO design, the color attribute could be moved up to a class called Mammal along with any other common attributes and methods. In this case, both Dog and Cat inherit from the Mammal class, as shown in Figure 1.
Figure 1: Mammal hierarchy.
The Dog and Cat classes both inherit from Mammal. This means that a Dog class actually has the following attributes:
eyeColor // inherited from Mammal barkFrequency // defined only for Dogs
In the same vein, the Dog object has the following methods:
getEyeColor // inherited from Mammal bark // defined only for Dogs
When the Dog or the Cat object is instantiated, it contains everything in its class, as well as everything from the parent class. Thus, Dog has all the properties of its class definition, as well as the properties inherited from the Mammal class.
Superclasses and Subclasses
The superclass, or parent class, contains all the attributes and behaviors that are common to classes that inherit from it. For example, in the case of the Mammal class, all mammals have similar attributes such as eyeColor and hairColor, as well as behaviors such as generateInternalHeat and growHair. All mammals have these attributes and behaviors, so it is not necessary to duplicate them down the inheritance tree for each type of mammal. Thus, the Dog and Cat classes inherit all those common attributes and behaviors from the Mammal class. The Mammal class is considered the superclass of the Dog and the Cat subclasses, or child classes.
Inheritance provides a rich set of design advantages. When you're designing a Cat class, the Mammal class provides much of the functionality needed. By inheriting from the Mammal object, Cat already has all the attributes and behaviors that make it a true mammal. To make it more specifically a cat type of mammal, the Cat class must include any attributes or behaviors that pertain solely to a cat.
An inheritance tree can grow quite large. When the Mammal and Cat classes are complete, other mammals, such as dogs (or lions, tigers, and bears), can be added quite easily. The Cat class can also be a superclass to other classes. For example, it might be necessary to abstract the Cat class further, to provide classes for Persian cats, Siamese cats, and so on. Just as with Cat, the Dog class can be the parent for GermanShepherd and Poodle (see Figure 2). The power of inheritance lies in its abstraction and organization techniques.
Figure 02: Mammal UML diagram.
Note that the classes GermanShepherd and Poodle both inherit from Dog each contains only a single method. However, because they inherit from Dog, they also inherit from Mammal. Thus, the GermanShepherd and Poodle classes contain all the attributes and methods included in Dog and Mammal, as well as their own (see Figure 3).
Figure 03: Mammal hierarchy.
Page 1 of 3