<< 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.

Questions & Answers

Biology is a branch of Natural science which deals/About living Organism.
Ahmedin Reply
what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
cell is the smallest unit of the humanity biologically
Abraham
what is biology
Victoria Reply
what is biology
Abraham
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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