Method Overriding in Java

Defining a method in a sub class which is already defined in the super class is called method overriding. The sub class can redefine the method in a way suited to its need. The method of the superclass is called overridden method and the method of the subclass is called overriding method.

Overriding methods in Java
Overriding getType() method in each subclass of the Animal class.


In the diagram, the superclass Animal provides the definition of the getType() method. But The subclasses redefine the same method with the details suited to their need. So the definition of getType() in the sub class override the superclass method.

  • The Animal’s getType() method is called the overridden method.
  • Lion’s and Cow’s getType() method is called the overriding method.

Method overriding is the primary way to achieve runtime polymorphism in Java.

1. An Example

Let’s convert the diagram above into classes.

Output :

Note that after assigning a Lion object to animal class reference, we didn’t have to cast the reference to a Lion type to call the getType() method of the Lion class. If the getType method weren’t declared in the super class, we would have to use casting before calling it.

2. Requirements for method overriding

  • They must have the same argument list
  • They must have the same return type or a subtype atleast. If a overriding method returns subtype then this subtype is called a covariant return type.
  • Overriding method must not be more restrictive.
  • The overriding method must not throw new or broader checked exceptions.

Let’s look at each one of them one at a time.

2.1 Overriding methods must have the same argument list

The Overriding method must have the same argument list as that of overridden method. If they don’t then the methods are overloaded not overridden.

Output :

Visit this article to know more about overloading vs overriding.

2.2 Overriding methods must have the same return type

Overriding method must have the same return type or a subtype atleast. When a subtype is  returned then this subtype is called a covariant return type.

Output :


2.3 Overriding methods must not be more restrictive

The overriding method must not re-declare its access level to a more restrictive access type. The restrictiveness of access modifiers goes like this:

  • private > default(no-keyword) > protected > public .
  • So private is most restrictive and public is least restricitve.

Output :


2.4 Exceptions and Overriding

Overriding methods must not throw new or broader checked exceptions.

So they can throw:

  • Any unckecked exception.
  • narrower and fewer checked exception than those defined by the super class.

Note : The difference between the two exception types is that the Checked exceptions are thrown at compile time while unckecked exceptions are thrown at run time.

3. Abstract methods are meant to be overridden

A method that has been declared abstract must be overridden in the subclass, if you want to use this class to create objects.

Output :

To know more about abstract keyword visit this article.

4. Methods that cannot be overridden

Some methods cannot be overridden. Let’s see them one at a time.

  • Private methods cannot be overridden: Since private methods aren’t inherited, they cannot be overridden.
  • Constructors cannot be overridden: Constructors are like methods with no return type. But they are not inherited, so they cannot be overriden.
  • final methods cannot be overridden: You cannot override a final method. This is how Java implements a final method. Their definition is final.
  • Static methods cannot be overridden: We cannot override a static method because it is bound to a class while overriding method is bound to an object at runtime.

Note: Since main method is also a static method, you cannot  override it either.

5. Method Overriding vs Method Hiding

A static method cannot be overriden but you can redefine the same static method in a sub class. In that case, the method in the subclass would hide the method in the super class (not override it).

The distinction between hiding a static method and overriding an instance method has important implications:

  • The version of the overridden instance method that gets invoked is the one in the subclass.
  • The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.

Output :


6. Use super keyword to invoke the overidden method

We can use super keyword to invoke the overridden method of the superclass from a subclass.

Output :


7. Override Annotation

If you override a method in a subclass and the method in the superclass is modified suddenly such that its name is changed or it is removed or its signature is changed then the method in the sub class no longer overrides the method in the super class. In such a case, you may expect the compiler to tell that no overridng occurs.

This is what Override annotation is built for. You can place the Override annotation above the overriding method.

Output :

8. An Example: Multilevel method overriding

Let’s have an example of 2 level overriding. Similarly, you can extend it to any level you want. There are only two things to remember:

  • The the type of the object reference is a parent class type.
  • The method that is called belongs to the type of subclass object this reference referes to.

Output :


9. Miscellaneous Topics

  • Synchronized method : Synchronized modifier is used in context of multi-threading. A Synchronized method can be overridden like a normal method. You can override a synchronized method with a non-synchronized method.
  • Strictfp Method : Like Synchronized, it has no effect on rules of overriding. You can override it like any normal method.