A subclass method can override a superclass method with the same name and parameter types.

A subclass can either completely override the implementation for an inherited method or the subclass can enhance the method by adding functionality to it.

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 an empty method implementation with the expectation that most, if not 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 can't possibly 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 a generic Thread without building a subclass). 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 behavior 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 sure your new subclass of Window also works 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 the 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 behavior 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's method, it's important to note that in a comment.

Methods a Subclass Cannot Override

  • A subclass cannot override methods that are declared final in the superclass (by definition, final methods cannot be overriden). If you attempt to override a final method, the compiler will display an error message similar to this one and refuse to compile the program:
    FinalTest.java:7: Final methods can't be overriden. Method void iamfinal() is final in class ClassWithFinalMethod. void iamfinal() { ^ 1 error
    For a discussion of final methods, see Writing Final Classes and Methods.
  • Also, a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. See Instance and Class Members for an explanation of class methods.

Methods a Subclass Must Override

Subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract. Writing Abstract Classes and Methods discusses abstract classes and methods in detail.

  1. Understanding the problem without method overriding
  2. Can we override the static method
  3. Method overloading vs. method overriding

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.

In other words, If a subclass provides the specific implementation of the method that has been declared by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding

  • Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
  • Method overriding is used for runtime polymorphism

Rules for Java Method Overriding

  1. The method must have the same name as in the parent class
  2. The method must have the same parameter as in the parent class.
  3. There must be an IS-A relationship (inheritance).
A subclass method can override a superclass method with the same name and parameter types.

Understanding the problem without method overriding

Let's understand the problem that we may face in the program if we don't use method overriding.

Test it Now

Output:

Problem is that I have to provide a specific implementation of run() method in subclass that is why we use method overriding.


Example of method overriding

In this example, we have defined the run method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method are the same, and there is IS-A relationship between the classes, so there is method overriding.

Test it Now

Output:


A real example of Java Method Overriding

Consider a scenario where Bank is a class that provides functionality to get the rate of interest. However, the rate of interest varies according to banks. For example, SBI, ICICI and AXIS banks could provide 8%, 7%, and 9% rate of interest.

A subclass method can override a superclass method with the same name and parameter types.

Java method overriding is mostly used in Runtime Polymorphism which we will learn in next pages.

Test it Now

Output: SBI Rate of Interest: 8 ICICI Rate of Interest: 7 AXIS Rate of Interest: 9

Can we override static method?

No, a static method cannot be overridden. It can be proved by runtime polymorphism, so we will learn it later.


Why can we not override static method?

It is because the static method is bound with class whereas instance method is bound with an object. Static belongs to the class area, and an instance belongs to the heap area.


Can we override java main method?

No, because the main is a static method.


Difference between method Overloading and Method Overriding in java

Click me for the difference between method overloading and overriding


More topics on Method Overriding (Not For Beginners)

Method Overriding with Access Modifier

Let's see the concept of method overriding with access modifier.

Exception Handling with Method Overriding

Let's see the concept of method overriding with exception handling.

Can subclass override the methods of superclass?

A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. A subclass in a different package can only override the non-final methods declared public or protected.

Can I override a method with different parameters?

Both methods will still exist, so it's called overloading. In Java and in C Sharp it works pretty much the same; you just define a new method with different parameters. You cannot just change the return type, though. The parameters to the methods must be different in order to overload one.

Which methods must be override in the subclass?

Subclass must override methods that are declared abstract in the superclass, or the subclass itself must be abstract. Writing Abstract Classes and Methods discusses abstract classes and methods in detail.

Can subclass override the static methods of superclass?

No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time.