getSpeed() );
}
This will be the new output:
3 Automobiles
2006 Kia Rio
2002 VW Passat
2005 Ford Mustang
2002 VW Passat 70.0
2005 Ford Mustang 120.0
Polymorphism allows us to write programs in a more general way. We can reference the superior class as
we write programs, and if the object being used by the program happens to belong to a subclass of the superior
CHAP. 5] PROGRAMMING IN JAVA 83
class, the methods in the subclass which override the corresponding methods in the superior class will insure that
methods appropriate to the particular instance will be called. This is another way in which OO programming
reduces the amount of code that must be written and tested, and also promotes reuse of existing code in new
applications.
INTERFACES
In Java, we can also specify an interface to enforce a common set of behaviors among different classes.
For example, think about the problem of sorting a group of Automobile objects. How should Automobiles
be ordered? By year? Alphabetically by make? By horsepower? This problem arises frequently when we??™re
working with objects, so the Java language specifies an interface that any new class can implement in order
to facilitate sorting instances of the new class.
Pages:
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228