Establishing Inheritance

You establish a class as the subclass of another class with the extends statement, as in the following:

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


The extends statement establishes the AnimatedLogo class of objects as a subclass of JApplet, using the full class name of javax.swing.JApplet. As you will 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 will have to override is the paint() method, which is used to draw all things that are shown on the program's window. The paint() method is implemented by the Component class and 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 when that subclass is running. To override a method, you must start the method in the same way it started 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 is in the name of the Graphics object, which does not matter when determining whether 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, this can be used, 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. super can be used 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 of the ways you'll use the super keyword is in the constructor method of a subclass. Because a subclass inherits all 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 won't be able to function properly. To make sure that this happens, 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, and it uses super(name, length) to call a comparable constructor in its superclass.

Did you Know?

If you don't use super to call a constructor method in the superclass, the program still might compile and run successfully. If no superclass constructor is called, Java automatically calls one with no arguments when the subclass constructor begins. If this superclass constructor doesn't exist or provides unexpected behavior, errors will result, so it's much better to call a superclass constructor yourself.


      
Comments