<< Chapter < Page Chapter >> Page >

The driver class

Listing 4 shows the code for the driver class named Radio03.

Listing 4 . The driver class.
public class Radio03{ //This class simulates the// manufacturer and the human user public static void main(String[] args){

The code in Listing 4 is also identical to the code in the program named Radio02 discussed in the previous module. Therefore, I won't discuss it in detail here.

A new object of the Combo class

I present this code here solely to emphasize that this code instantiates a new object of the Combo class. This assures that the overridden version of the method named playStation will be executed by the statements in Listing 4 that call the playStation method.

(Although it is not the case in Listing 4 , even if the reference to the object of type Combo had been stored in a reference variable of type Radio , instead of a reference variable of type Combo , calling the playStation method on that reference would have caused the overridden version of the method to have been executed. That is the essence of runtime polymorphism based on overridden methods in Java.)

Program output

This program produces the output shown in Figure 1 on the computer screen.

Figure 1 . Program output.
Combo object constructed Play RadioPlaying the station at 93.5 Mhz Insert TapeTape is in Radio is offPlay Radio Remove the tape firstRemove Tape Tape is outRadio is on Play RadioPlaying the station at 93.5 Mhz Play TapeInsert the tape first Insert TapeTape is in Radio is offPlay Tape Tape is playingRemove Tape Tape is outRadio is on Play RadioPlaying the station at 93.5 Mhz

I will leave it as an exercise for the student to compare this output with the messages sent to the object by the code in Listing 4 .

Summary

An object instantiated from a class that extends another class will contain all of the methods and all of the variables defined in the subclass, plus all of the methods and all of the variables inherited into the subclass.

The behavior of methods inherited into the subclass may not be appropriate for an object instantiated from the subclass. You can change that behavior by overriding the method in the definition of the subclass.

To override a method in the subclass, reproduce the name, argument list, and return type of the original method in a new method definition in the subclass. Make sure that the overridden method is not less accessible than the original method. Also, make sure that it doesn't throw any checked exceptions that were not declared for the original method.

Provide a body for the overridden method, causing the behavior of the overridden method to be appropriate for an object of the subclass. Any method that is not declared final can be overridden in a subclass. The program discussed in this module extends a Radio class to produce a subclass that simulates an upgraded car radio containing a tape player.

Method overriding is used to modify the behavior of an inherited method named playStation to cause that method to behave appropriately when a tape has been inserted into the radio.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask