posted 21 years ago
Consider the code below, and note the difference between overridding and overloading.
We have a Car class and a subclass of Car called VWJetta. Note that the method announce() is overridden in these classes.
In the class Garage, we have a method called polymorhpicAnnouncer, which takes a Car as an argument and calls that Car's announce method. To the compiler, this object is always a Car, and if we pass an instance of VWJetta, it will be automatically converted to type Car (upcast through method-call conversion). But when that Car's method is called, the object's true runtime type will be used to invoke the appropriate method body.
In Garage, we also have a method called signatureAnnouncer, which is overloaded to take different argument types: One method signature is for an argument type of Car, and the other is for the "more specific" argument type of VWJetta. In this situation, the compiler selects the appropriate method based on the compile-time type of the agrument being passed.
In main, we create an instance of VWJetta (its true runtime type), then pass that to the constructor for Garage, where the reference is upcast to type Car (its compile-time type within that scope)...
[ January 20, 2005: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org