Many times a new
programming effort is, ???the same as the last effort, but different ...??? When that is the case, it??™s helpful to take
advantage of the earlier code by creating a new class that inherits from the old, and simply adds the new features.
For instance, if a program requires a limousine object, the limousine class might be designed as a subclass
of automobile. A limousine might have additional instance variables, or attributes, related to cost, beverages on
board, schedule, etc. Otherwise, the limousine class could take advantage of the color, horsepower, and speed
attributes already defined for automobiles. A limousine could also share the behavior of automobiles via the
changeSpeed, park, and other methods already defined.
In describing inheritance, computer scientists say that the relationship between a subclass and a superior
class constitutes an ???is-a??? relationship. The subclass is also an instance of the superior class. The limousine is
also an automobile.
Inheritance allows one class to take advantage of the attributes and behavior of another. Often the design of
an OO programming project is focused in large part on the class hierarchy (the set of classes to be written), and
the relationships via inheritance of the classes to one another.
Pages:
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150