Establishing Inheritance

A class is defined as the subclass of another class using the extends statement, as in the following:

class AnimatedLogo extends JApplet {
    // behavior and attributes go here
}

The extends statement establishes the AnimatedLogo class of objects as a subclass of JApplet. As you see during Hour 17, “Creating Interactive Web Programs,” all Swing applets must be subclasses of JApplet. They need the functionality this class provides to run when presented on a web page.

One method that AnimatedLogo must override is the paint() method, which is used to draw everything within the program’s window. The paint() method, implemented by the Component class, is passed all the way down to AnimatedLogo. However, the paint() method does not do anything. It exists so that subclasses of Component have a method they can use when something must be displayed.

To override a method, you must declare the method in the same way it was declared in the superclass from which it was inherited. A public method must remain public, the value sent back by the method must be the same, and the number and type of arguments to the method must not change.

The paint() method of the Component class begins as follows:

public void paint(Graphics g) {

When AnimatedLogo overrides this method, it must begin with a statement like this:

public void paint(Graphics screen) {

The only difference lies in the name of the Graphics object, which does not matter when determining if the methods are created in the same way. These two statements match because the following things match:

• Both paint() methods are public.

• Both methods return no value, as declared by the use of the void statement.

• Both have a Graphics object as their only argument.

Using this and super in a Subclass

Two keywords that are extremely useful in a subclass are this and super.

As you learned during the previous hour, the this keyword is used to refer to the current object. When you’re creating a class and you need to refer to the specific object created from that class, you can use this, as in the following statement:

this.title = "Cagney";

This statement sets the object’s title variable to the text “Cagney.”

The super keyword serves a similar purpose: It refers to the immediate superclass of the object. You can use super in several different ways:

• To refer to a constructor method of the superclass, as in super(“Adam”, 12);

• To refer to a variable of the superclass, as in super.hawaii = 50

• To refer to a method of the superclass, as in super.dragnet()

One way you use the super keyword is in the constructor method of a subclass. Because a subclass inherits the behavior and attributes of its superclass, you have to associate each constructor method of that subclass with a constructor method of its superclass. Otherwise, some of the behavior and attributes might not be set up correctly, and the subclass isn’t able to function properly.

To associate the constructors, the first statement of a subclass constructor method must be a call to a constructor method of the superclass. This requires the super keyword, as in the following statements:

public readFiles(String name, int length) {
    super(name, length);
}

This example is the constructor method of a subclass, which is using super(name, length) to call a comparable constructor in its superclass.

If you don’t use super to call a constructor method in the superclass, Java automatically calls super() with no arguments when the subclass constructor begins. If this superclass constructor doesn’t exist or provides unexpected behavior, errors result, so it’s better to call a superclass constructor yourself.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.236.27