The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Classes and Inheritance

Using super

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of super. You can also use super to refer to a hidden member variable. Consider this class, Superclass:
public class Superclass {
    public boolean aVariable;

    public void aMethod() {
        aVariable = true;
    }
}
Now, here's a subclass, called Subclass, that overrides aMethod and hides aVariable:
public class Subclass extends Superclass {
    public boolean aVariable; //hides aVariable in Superclass
    public void aMethod() { //overrides aMethod in Superclass
        aVariable = false;
        super.aMethod();
        System.out.println(aVariable);
        System.out.println(super.aVariable);
    }
}
Within Subclass, the simple name aVariable refers to the one declared in SubClass, which hides the one declared in Superclass. Similarly, the simple name aMethod refers to the one declared in Subclass, which overrides the one in Superclass. So to refer to aVariable and aMethod inherited from Superclass, Subclass must use a qualified name, using super as shown. Thus, the print statements in Subclass's aMethod display the following:
false
true
You can also use super within a constructor to invoke a superclass's constructor. The following code sample is a partial listing of a subclass of Thread — a core class used to implement multitasking behavior — which performs an animation. The constructor for AnimationThread sets up some default values, such as the frame speed and the number of images, and then loads the images:
class AnimationThread extends Thread {
    int framesPerSecond;
    int numImages;
    Image[] images;

    AnimationThread(int fps, int num) {
        super("AnimationThread");
        this.framesPerSecond = fps;
        this.numImages = num;
        this.images = new Image[numImages];

        for (int i = 0; i <= numImages; i++) {
            ...
            // Load all the images.
            ...
        }
    }
    ...
}
The line set in boldface is an explicit superclass constructor invocation that calls a constructor provided by the superclass of AnimationThread, namely, Thread. This particular Thread constructor takes a String that sets the name of Thread. If present, an explicit superclass constructor invocation must be the first statement in the subclass constructor: An object should perform the higher-level initialization first. If a constructor does not explicitly invoke a superclass constructor, the Java runtime system automatically invokes the no-argument constructor of the superclass before any statements within the constructor are executed.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.