<< Chapter < Page Chapter >> Page >

The Combo class

In this module, I will use inheritance to extend the Radio class into a new class named Combo . Objects instantiated from the Combo class are intended to simulate car radios with a built-in tape player.

A complete listing of the new program is shown in Listing 9 near the end of the module.

Will discuss in fragments

As usual, I will discuss this program in fragments. I will begin my discussion with the definition of the new class named Combo . Then I will come back and discuss the class named Radio and the driver class named Radio02 .

The combo class

The code in Listing 1 shows the beginning of the class named Combo .

Listing 1 . Beginning of the Combo class.
class Combo extends Radio{public Combo(){//constructor System.out.println("Combo object constructed"); }//end constructor

Two new items

There are two new items in Listing 1 that you did not see in the code in the previous modules.

Combo extends Radio

First, the class named Combo extends the class named Radio . This means that an object instantiated from the Combo class will contain all of the variables and all the methods defined in the Combo class, plus all the variables and methods defined in the Radio class, and its superclasses. (The variables and methods of the superclass are inherited into the subclass.)

An explicit constructor

Second, the class named Combo defines an explicit constructor.

Defining a constructor is optional

When defining a new class, it is not necessary to define a constructor. If you don't define a constructor, a default constructor will be provided automatically.

Why define a constructor?

The intended purpose of a constructor is to initialize the instance variables belonging to the new object. However, constructors can do other things as well. In this case, I used an explicit constructor to display a message when the object is instantiated from the class named Combo .

Brief discussion of constructors

I'm not going to discuss constructors in detail at this point. However, I will give you a few rules regarding constructors.

  • Constructors (like methods) can be overloaded. (I will explain what overloading means in a subsequent module.)
  • The names of constructors must match the names of the classes in which they are defined.
  • A constructor signature never indicates a return type (such as void or double) .
  • The code in a constructor never contains a return statement.

Instance methods

The new class named Combo defines three instance methods, each of which has to do with the handling of tape in the tape player:

  • insertTape
  • removeTape
  • playTape

(If you feel ambitious, you could upgrade this class even further to add features such as rewind, fast forward, pause, etc.).

The insertTape method

The entire method named insertTape is shown in Listing 2 . This is the method that is used to simulate the insertion of a tape by the user.

Listing 2 . The insertTape method.
public void insertTape(){ System.out.println("Insert Tape");tapeIn = true

The most significant thing about the code in Listing 2 is the assignment of the true value to the boolean variable named tapeIn . Other than setting the value of the tapeIn variable to true , the code in Listing 2 simply prints some messages to indicate what is going on.

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