<< Chapter < Page | Chapter >> Page > |
Any method that is not declared final can be overridden in a subclass.
Overriding versus overloading
Don't confuse method overriding with method overloading . Here is what Roberts, Heller, and Ernest have to say about overloading methods:
"A valid overload differs in the number or type of its arguments. Differences in argument names are not significant. A different return type is permitted, but is not sufficient by itself to distinguish an overloading method."
Car radios with built-in tape players
This module presents a sample program that duplicates the functionality of the program named Radio02 discussed in the previous module. A class named Radio is used to define the specifics of objects intended to simulate car radios.
A class named Combo extends the Radio class to define the specifics of objects intended to simulate improved car radios having built-in tape players.
Modification of the superclass
In the program named Radio02 in the previous module, it was necessary to modify the superclass before extending it to provide the desired functionality. (The requirement to modify the superclass before extending it seriously detracts from the benefits of inheritance.)
No superclass modification in this module
The sample program (named Radio03 ) in this module uses method overriding to provide the same functionality as the previous program named Radio02 , without any requirement to modify the superclass before extending it. (Thus this program is more representative of the benefits available through inheritance than was the program in the previous module.)
Overridden playStation method
In particular, a method named playStation , defined in the superclass named Radio , is overridden in the subclass named Combo .
The original version of playStation in the superclass supports only radio operations. The overridden version of playStation defined in the subclass supports both radio operations and tape operations.
(The behavior of the version of playStation defined in the Radio class is not appropriate for an object of the Combo class. Therefore, the method was overridden in the Combo class to cause its behavior to be appropriate for objects instantiated from the Combo class.)
A complete listing of the program is shown in Listing 5 near the end of this module.
The class named Radio
As usual, I will discuss the program in fragments.
Listing 1 shows the superclass named Radio . This code is shown here for easy referral. It is identical to the code for the same class used in the program named Radio01 discussed in an earlier module.
Listing 1 . The class named Radio. |
---|
class Radio{
protected double[]stationNumber =
new double[5];public void setStationNumber(
int index,double freq){stationNumber[index] = freq;}//end method setStationNumber
public void playStation(int index){System.out.println(
"Playing the station at "+ stationNumber[index]
+ " Mhz");}//end method playStation |
Will override playStation
The class named Combo (discussed below) will extend the class named Radio . The method named playStation , shown in Listing 1 , will be overridden in the class named Combo .
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?