The SportsCar class must also have a constructor; it??™s called SportsCar. The constructor for
SportsCar simply uses the constructor for Automobile, the superclass, by using the super key word to
pass the same values for make, model, year, and power to the constructor for the Automobile class.
The only difference between instances of SportsCar and instances of Automobile will be the
behavior provided by the accelerate() method. In the case of an Automobile, the maximum speed will
be 70, but in the case of a SportsCar, the maximum speed will be 150. We say that the subclass SportsCar
overrides the accelerate() method inherited from the superclass Automobile.
Everything else that is true about an Automobile object will be true about a SportsCar object.
All will have instance variables to store make, model, year, horsepower and speed. Creating either a new
82 PROGRAMMING IN JAVA [CHAP. 5
Automobile or a new SportsCar will increment the count of Automobiles maintained by the
Automobile class.
We didn??™t have to change any of our existing code to enhance our work to treat sports cars differently from
other automobiles!
POLYMORPHISM
The word polymorphism means ???many forms.??? When a subclass overrides a method of a superior class, the
behavior of the method will depend upon which type of object is being used in the program, an instance of the
superior class or an instance of the subclass.
Pages:
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226