The method appears to be the same for both types of object, but
the execution differs depending on the class to which the object belongs. The park() method takes different
forms.
Polymorphism allows programmers to think about their programs in natural ways. For instance, lawnmowers,
cars, boats, and diesel trucks all must be ???started,??? but the means of starting the different machines can be very different.
Suppose that each type is a subclass of vehicle. Rather than write a differently named procedure for each activity,
the class designer simply implements different start() methods in the different classes, as appropriate. Then
the user of the classes can naturally invoke the start() method of the lawnmower object and the start()
method of the automobile object, without confusion and without complexity of naming. If the object being
started is a lawnmower, starting may involve pulling a rope; if it is an automobile, starting will involve turning
a key.
Variables such as color, horsepower, and speed, which comprise elements of the state of an individual
object, are called instance variables. Likewise, methods such as changeSpeed() and park(), which affect
the state of an individual object, are called instance methods.
Pages:
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152