Method Overriding in Java

In this lesson of our Java course, we will take a look at the method overriding in Java. Method overriding is an essential feature of Java that allows a subclass to provide its own implementation of a method that is already defined in its parent class. We will look at some important points along with some example to understand method overriding in java.

1. Method Overriding in Java

Method overriding is a feature of Java that allows a subclass to provide its own implementation of a method that is already defined in its parent class.Method overriding is a way of providing a more specific implementation of a method that is already defined in a superclass. This is really useful feature which gives us the flexibility to customize the behavior of the method as per our requirement or need. The method overriding in Java gives us the flexibility to inherit the features from other classes and at the same time gives the flexibility to customize the behavior. Here are some rules to remember while using Java method overriding.

  • The method name must be same as defined in the parent class. We can only override the method defined in the parent class.
  • Method overridden in the child class must have same parameters as the method defined in the parent class.
  • Return type of the child class method and parent class should be same.
Method Overriding in Java
Method Overriding in Java
If method don’t meet above criteria, most likely you are overloading the method and not overriding.

2. Java Method Overriding Example

It’s always good to learn using some example. We will use the same Car example which we have used in the previous lessons for better understanding. Let’s say we have a parent class called Vehicle and it’s subclass as Audi. The Vehicle class has a method called 'displayName(final String name)`. Our Audi class override the displayName(final String name) method and provides it’s own implementation.

public class Vehicle {
    public void displayName(String name) {
        System.out.println("Vehicle name:: "+name);
    }
}
public class Audi extends Vehicle {
    @Override
    public void displayName(String name) {
        System.out.println("Welcome to the Audi World and congratulation on your new car:: "+name);
    }
}

Let’s run this example to understand how method overriding works in Java

public static void main(String[] args) {
    Vehicle vehicle = new Vehicle();
    vehicle.displayName("My Car"); // Output: Vehicle name:: My Car
    
    Audi audi = new Audi();
    audi.displayName("Q5"); // Output: Welcome to the Audi World and congratulation on your new car::Q5
}

As you can see, method overriding provides us the flexibility to give our own implementation in the Audi class which is more aligned with the Audi brand.

3. How does Method Overriding work?

Method overriding in Java in a runtime polymorphism. When we call a method using a Java object, the Java virtual machine (JVM) check for the method in certain way.

  1. JVM first check if the method is defined in the class (whose object is created).
  2. If it does not find in the class, it checks if the method is defined in the parent class and call the parent class method. This process continues until the method’s implementation is found.

Let’s see rewrite the above example and understand it more clearly

class Vehicle {
    public void displayName(String name) {
        System.out.println("Vehicle name:: " + name);
    }
}
class Audi extends Vehicle {
    //this is not overriding as per the rules
    public void displayName() {
        System.out.println("Welcome to the Audi World and congratulation on your new car");
    }
}
public static void main(String[] args) {
    Audi audi = new Audi();
    audi.displayName("Q5");
}

Here is the output

Vehicle name:: Q5

As we explained above, the JVM will start from the actual object (Audi class object in this case) and will try to find the method. In our case the method is not defined in the Audi class, so JVM will go to the parent class (Vehicle) and search for the displayName method.

When a subclass overrides a method, it provides a new implementation for that method. When an object of the subclass is instantiated, the new implementation provided by the subclass is used instead of the implementation provided by the parent class. This is known as dynamic method dispatch.

4. Benefits of Java Method Overriding

There are multiple benefits of Method overloading in Java and it’s one of the core concept of object oriented programming in Java.

  • Reusability: Reusability is one of the main benefits of method overloading. We can use the parent class to define all common method and can continue to use the in the child class without defining them in each and every child class. Think if you have to provide definition of each method defined in the parent class :).
  • Flexibility: Method overloading provides a lot of flexibility. We use the parent class to define all the common method but at the same time, it give us the flexibility to override/ customize the method behavior in child class as per the need or requirement.
  • Extensibility: Method overriding allows us to extend a class. We can add new methods to a class by overriding its existing methods. This can be useful for adding new features to a class or for adapting a class to different needs.
  • Performance: Modern Java compiler are highly optimized, method overriding can improve the java program performance as it gives the compiler the option to optimize the code for each subclass that overrides the method.

4.1. Drawback of method overriding

While method overriding is a powerful tool in Java, it carry certain drawbacks as well

  • Complexity: If your parent class has a lot of method (e.g. something like Java String class) and let’s say this class is extended by a child class with method overridden in child class, it can become difficult to keep track of which method is overridden and which is not.
  • Method Visibility: if child class override parent class method, it will become hidden, this can be tricky especially if we need the method defined in the parent class.
  • Conflict: f two or more methods in a class are overridden, there is a risk that they will conflict with each other. For example, if two methods both have the same name and the same parameters, but different return types, then the compiler will not know which method to call.
  • Overhead: Though the overriding gives compiler the opportunity to optimize the code for each subclass but it will come with some overhead as compiler need to generate code for each class. This is not a big drawback as compare to the benefit it bring but we should keep this in mind to stop overusing overriding.

Summary

Method overriding is a powerful feature of Java that allows a subclass to provide its own implementation of a method that is already defined in its parent class. By understanding how method overriding works, you can create more flexible and reusable code. Make sure we understand some of the drawback created by method overdoing in Java before we use this feature. As always, the source code for this Java course is available on our GitHub Repository.

Scroll to Top