Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Overriding Methods

The ability of a subclass to override a method in its superclass allows a class to inherit from a superclass that is "close enough" in functionality and then supplement or modify the behaviour of that superclass.

Replacing a Superclass's Method Implementation

Sometimes, a subclass will want to replace entirely its superclass's implementation of a method. Indeed, many superclasses provide empty method implementation with the expectation that most, if not all, all subclasses will completely replace the superclass's implementation of the method.

One example of this is the run() method in the Thread class. The Thread class provides an empty implementation (the method does nothing) of the run() method because by definition the run() method is subclass dependent. The Thread class couldn't even provide a reasonable default implementation for the run() method. However, the run() method cannot be abstract because it also does not make sense for the Thread class to be abstract (programmers should be able to instantiate Thread). Thus, the implementation of run() is empty.

To completely replace a superclass's method implementation, simply name your method the same as the superclass method and provide the overriding method with the same signature as the overriden method.

class BackgroundThread extends Thread {
    void run() {
        . . .
    }
}
The BackgroundThread class overrides the run() method from its superclass Thread and completely replaces Thread's implementation of it.

Adding to a Superclass's Method Implementation

Other times a subclass will want to keep its superclass's implementation of a method but enhance it further with behaviour specific to the subclass. For example, constructor methods within a subclass typically do this--the subclass wants to preserve the initialization done by the superclass, but provide additional initialization specific to the subclass.

Suppose that you wanted to create a subclass of the Window class in the java.awt package. The Window class has one constructor that requires a Frame argument which is the parent of the window.

public Window(Frame parent)
This constructor performs some initialization on the window such that it will work within the window system. To make your new subclass of Window work within the window system, you too must provide a constructor for your window Subclass that performs the same initialization. Rather than attempt to figure out and recreate that initialization process that occurs within the Window constructor, you would much rather just use what the Window class already does. You can leverage the code in the Window constructor simply by calling it from within your Window subclass constructor
class RoundWindow extends Window {
    public RoundWindow(Frame parent) {
        super(parent);
        . . .
            // RoundWindow specific initialization here
        . . .
    }
}
The RoundWindow() constructor calls the superclass's constructor first--before it does anything else. Typically, this is the desired behaviour in constructors--the superclass should get the opportunity to perform all its initialization before the subclass. Other types of methods may wish to call the superclass's implementation of the method at the end of the subclass's method or in the middle of it. If the positioning of the call to the superclass's method is critical to the successful operation of the subclass method, it's important to note that in a comment.

Methods a Subclass Cannot Override

Methods a Subclass Must Override

Subclass must override methods that are declared abstract in the superclass, or the subclass must be abstract. [PENDING: say more? or refer to abstract section?]


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces