What is method overriding in Java?

A brief introduction, implementation, benefits, and limitations of method overriding

Method overriding is a feature that allows a subclass, or a child class, to specifically implement a method already given in one of its super-classes, or parent classes, in any object-oriented programming language.

Thus, the process of redefining a parent class’s method in a subclass is known as method overriding. It is also called run time polymorphism or dynamic binding because the compiler doesn’t really know the type of object passed on compilation.

When a method in a subclass has the same name, parameters or signature, and return type (or sub-type) as a method in its super-class, then the method in the subclass (the child class) overrides the method in the super-class (the parent class).

We can implement method overriding in any object-oriented programming language, but only when the classes involved have an ‘IS-A’ relationship of inheritance between them.

In the illustration above, the Rectangle and Circle classes are overriding the Shape of the class’s getArea() method. The purpose of overriding is achieved so that a sub class can provide its own implementation to a method that a superclass already provides.

class Animal {
public void eat(){
System.out.println("Eat all eatables");
}
}
class Dog extends Animal {
//eat() method overridden by Dog class.
public void eat(){
System.out.println("Dog likes eating bones");
}
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
}
}

In the above example, the Dog class gives its own implementation of the eat() method. For method overriding, the method must have same name and same type signature in both the superclass and the subclass.

Method overriding and dynamic method dispatch

To accomplish Java runtime polymorphism, overriding methods are utilized. The object that is used to call the method determines the version of the method that is being run. When a method is called with an object from a superclass, the parent class’s version is executed, but when a method is called with an object from a subclass, the child class’s version is executed.

That is, which version of the overridden method is performed is determined by the type of the referenced object (not the type of the referenced variable). The practice of resolving overridden method calls at runtime is known as dynamic method dispatch. To understand this, see the example below:

class Mother{
//Overridden method
public void smile(){
System.out.println("The Mother smiling");
}
}
class Baby extends Mother{
//Overriding method
public void smile(){
System.out.println("The Baby is smiling");
}
// New method is Baby
public void tickle(){
System.out.println("The Baby is tickling");
}
}
class Main{
public static void main( String args[]) {
Mother call = new Mother();
Mother call1 = new Baby();
//This will call the child class version of smile()
call1.smile();
//This will call the Mother class version of smile()
call.smile();
}
}

In the example above, the smile() method call with the second object (call1) is runtime polymorphism (or dynamic method dispatch).

Note: In dynamic method dispatch, an object can call overriding methods of child classes and all non-overriding methods of base classes, but not newly declared methods in child classes. In the example above, object call1 calls smile(). However, when I try to call the tickling() method (newly declared in class Baby) using obj2, I get a compilation error with the following message: Thread “main” exception java.lang.Error: unresolved compilation Problem: Method tickle() is not defined for type Mother

Inheritance and final keyword

For method overriding to happen, there has to be an IS-A relationship between a baseclass and a subclass. This is known as inheritance in OOP concept. When there is an IS-A relationship between two classes, the child class can therefore override the methods it inherits from the base class.

During inheritance, we have to declare methods with the final keyword which we required to follow the same implementation throughout all the derived classes. When a method is declared as final, it cannot be overridden by subclasses. The classes that extend from a base class that have methods with final keywords can only implement the method same way it was in the super class.

Covariant in method overriding

If a subclass overrides any method with a Non-Primitive return type, the method can be overridden by modifying the return type.

It is possible for a child class to have a distinct return type for an overriding method, but the child’s return type must be a sub-type of the parent’s return type.

class Base{
Base get(){
return this;
}
}
class Child extends Base{
@Override
Child get(){
return this;
}
void message(){
System.out.println("welcome to covariant return type");
}
public static void main(String args[]){
new Child().get().message();
}
}

Benefits of method overriding in Java

  • It is used for the implementation of runtime or dynamic polymorphism.
  • It is used to provide a specific implementation or definition of a method in a class, which is already in an existence in its superclass.
  • It is also used to define what behavior a class can have and how that behavior will be implemented by the class that will inherits it.

Method overriding limitations

  • Both the parent class and the child class must have the same method name, the same return type and the same parameter list.
  • Methods declared final and static cannot be overridden.
  • A method cannot be overridden if it cannot be inherited. That is, it must be an IS-A relationship.
  • The access level cannot be more restrictive than the overridden method’s access level.
  • The argument list should match the overridden method.
  • Constructors cannot be overridden.
  • The abstract method of the super class will always be overridden.

Co-authored by Matthew Adesina

Free Resources