<< Chapter < Page Chapter >> Page >

If you examine the code for the playStation method in Listing 1 , you will see that it assumes radio operations only and doesn't support tape operations. That is the reason that it needs to be overridden. (For example, it doesn't know that it should refuse to play a radio station when a tape is being played.)

The Combo class

Listing 2 shows the beginning of the class definition for the class named Combo . The Combo class extends the class named Radio .

Listing 2 . Beginning of the Combo class.
class Combo extends Radio{private boolean tapeIn = false;

The tapeIn variable

The most important thing about the code in Listing 2 is the declaration of the instance variable named tapeIn .

(In the program named Radio02 in the previous module, this variable was declared in the class named Radio and inherited into the class named Combo . That was one of the undesirable changes required for the class named Radio in that module.)

In this version of the program, the variable named tapeIn is declared in the subclass instead of in the superclass. Thus, it is not necessary to modify the superclass before extending it.

The constructor

The constructor in Listing 2 is the same as in the previous program named Radio02 , so I won't discuss it further.

The overridden playStation method

The overridden version of the method named playStation is shown in Listing 3 . As you can see, this version of the method duplicates the signature of the playStation method in the superclass named Radio , but provides a different body.

Listing 3 . The overridden playStation method.
public void playStation(int index){ System.out.println("Play Radio");if(!tapeIn){//tapeIn is false System.out.println(" Playing the station at " + stationNumber[index]+ " Mhz"); }else{//tapeIn is trueSystem.out.println( " Remove the tape first");}//end if/else }//end method playStation

Aware of the tape system

This overridden version of the playStation method in Listing 3 is aware of the existence of the tape system and behaves accordingly.

Depending on the value of the variable named tapeIn , this method will either

  • tune and play a radio station, or
  • display a message instructing the user to remove the tape.

Which version of playStation is executed?

When the playStation method is called on an object of the Combo class, the overridden version of the method (and not the original version defined in the superclass named Radio ) is the version that is actually executed.

Although not particularly obvious in this example, this is one of the important characteristics of runtime polymorphism . When a method is called on a reference to an object, it is the type of the object (and not the type of the variable containing the reference to the object) that is used to determine which version of the method is actually executed.

Three other instance methods

The subclass named Combo defines three other instance methods:

  • insertTape
  • removeTape
  • playTape

The code in these three methods is identical to the code in the methods having the same names in the program named Radio02 in the previous module. I discussed that code in the previous module and won't repeat that discussion here. You can view those methods in the complete listing of the program shown in Listing 5 near the end of this module.

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