Method Overriding in Java

Method Overriding in Java allows a subclass (or child class) to provide a specific implementation of a method which is already defined in its super class (or parent class). When a method in a subclass has the same name, parameters and return type as the method defined in its super class, then the subclass class overrides the method of the superclass.

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

Method overriding in java is used to achieve Run Time Polymorphism. It means, is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time.

Which version of the method needs to be executed is determined by the object that is used to invoke it. If an object of a super class is used to invoke the method, then the method version in the super class is executed whereas if an object of the subclass is used to invoke the method, then the method version in the child class is executed.


Java program for method overriding

class Bird {
  String birdtype;

  Bird(String birdtype) {
    this.birdtype = birdtype;
  }
  public void move() {
    System.out.println(birdtype + " is Flying");
  }
}

class Ostrich extends Bird {
  Ostrich(String birdtype) {
    super(birdtype);
  }
  public void move() {
    System.out.println(birdtype + " is Running");
  }
}

public class MethodOverriding {
  public static void main(String[] args) {
    // Create object of parent class
    Bird bird = new Bird("Crow");
    // Create object of child class
    Ostrich ostrich = new Ostrich("Ostrich");
    // Invoke move() of parent class
    bird.move();
    // Invoke move() of child class
    ostrich.move();
  }
}
Output
Crow is Flying
Ostrich is Running

In the above program, the move() method is present in both Bird superclass and Ostrich subclass. As we know, that Ostriches cannot fly like other birds. Hence, we have overriden the move() method in Ostrich class.

When we call move() using the bird object (object of the super class), the method inside the Bird is invoked whereas when we call move() using ostrich object (object of the sub class) the method inside the Ostrich is invoked.


Rules for Method Overriding in Java

  • The method name, argument list and return type of method in child class should be exactly the same as the original overridden method in the parent class.

  • A method declared final in parent class cannot be overridden.

  • A method declared static in parent class cannot be overridden.

  • Constructors cannot be overridden.

  • If a method cannot be inherited, then it cannot be overridden.

  • If a class is extending an abstract class or implementing an interface then it has to override all the abstract methods unless the class itself is a abstract class.

Invoking overridden method of parent class

As we know that, when we override a method in child class, then call to the method using child class object calls the overridden method of child class. A common question that arrises in our mind is

Can we access the overridden method of the parent class from child class ?

Yes, we can. We can call parent class overridden method from child class using super keyword. Here is a java program to access overridden method of parent class from child class.

class Driver {
  // overridden method
  public void getInfo() {
    System.out.println("I am a Driver.");
  }
}

class TruckDriver extends Driver {
  // overriding getInfo method
  public void getInfo() {
    // calling base class overridden method.
    super.getInfo();
    System.out.println("I am Driving a Truck.");
  }
}
public class MethodOverridingSuper {
  public static void main(String[] args) {
    TruckDriver truckDriver = new TruckDriver();
    // Calling overridden method
    truckDriver.getInfo();
  }
}
Output
I am a Driver.
I am Driving a Truck.

In the above program, the sub class TruckDriver overrides the getInfo() method of superclass Driver. Inside getInfo() method of TruckDriver class, we used super.getInfo() to call getInfo() method of the super class Driver.