Java method overriding explanation with example

An introduction to method overriding in Java:

Inheritance is one of the important features of Object Oriented Programming(OOP). With this concept, one class inherits the methods and attributes of another class. The original class is called parent class and the second class is called child class. Child class inherits the methods and attributes of the parent class. Parent and Child classes are also called super class and sub class.

If the child class has the same method as the parent class, it is called method overriding. In this blog post, we will learn how method overriding works in Java with different examples.

Rules of method overriding:

Following are the rules of method overriding:

  • Methods that are declared as final and static are not available for override.
  • Abstract methods of superclass should be always overridden.
  • For all overridden methods, the method name, return type and parameters list shouldn’t be different in both parent and child classes.

Example of method overriding:

Let’s take an example of method overriding:

class Vehicle {
    public void print() {
        System.out.println("Inside Vehicle");
    }
}

class Car extends Vehicle {
    @Override
    public void print() {
        System.out.println("Inside Car");
    }
}


public class Main {
    public static void main(String[] args) {
        Car car = new Car();
        car.print();
    }
}
  • The Vehicle class is the parent class and the Car class is the child class. The Car class is extending the Vehicle class.
  • The print method is defined in the Vehicle class. Since the Car class is extending the Vehicle class, we can also write the same method in the Car class.
  • Inside the Main class, we are creating one object of the Car class and calling the print method. It will call the overriding print method, i.e. the method defined in the Car class.

It will print:

Inside Car

It is using the @Override annotation to define that the method is overriding its parent class method. It is not mandatory to use this annotation but if we use it, the method should follow the overriding rules we have mentioned before.

How to access the method of superclass inside the overriding method:

We can access the method of the superclass inside the overriding method. We have to use the super keyword for that. For example:

class Vehicle {
    public void print() {
        System.out.println("Inside Vehicle");
    }
}

class Car extends Vehicle {
    @Override
    public void print() {
        super.print();
        System.out.println("Inside Car");
    }
}


public class Main {
    public static void main(String[] args) {
        Car car = new Car();
        car.print();
    }
}

Inside the override print() method, we are calling super.print(), which will call the print method in the superclass.

If you run this program, it will print:

Inside Vehicle
Inside Car

As you can see, the first line is printed by the print method defined in the superclass Vehicle. The second line is printed by the print method defined in the class Car.

How Access Specifier works in Overriding:

The access specifier of an overriding method can be different that the one defined in the superclass. But its access shouldn’t be less than the one defined in the superclass.

For example, if the method of the superclass is protected, the override method can be either public or protected, not private. Else, it will throw a compile-time error.

Let’s take a look at the below example:

class Vehicle {
    protected void print() {
        System.out.println("Inside Vehicle");
    }
}

class Car extends Vehicle {
    @Override
    private void print() {
        super.print();
        System.out.println("Inside Car");
    }
}

The Vehicle class is the parent class and the Car class is the child class. The print method in the Vehicle class is protected but the override method in the Car class is private.

It will throw one compile-time error as below:

Java access modifier example

Since the access modifier in the parent class is protected, we can’t use private in the child class.

Method overriding of static method:

We can’t override a static method because the static method is a class method, not an object method. Similarly, we can’t override a main method because it is a static method.

You might also like: