July 8, 2022

Method Overriding in Java

When a method in child class has the same name and the same signature (same number and type of parameters) as the method in the parent class then the sub class is overriding the method in the super class and this process is known as method overriding in Java.

Example of method overriding in Java

class A {
  public int add(int a, int b) {
    System.out.println("in add method of the parent class" );
    return a+b;
  }
}

public class B extends A{
  // Overriding method of the parent class
  public int add(int a, int b) {
    System.out.println("in add method of the child class" );
    return a+b;
  }	

  public static void main(String[] args) {
    B obj = new B();
    System.out.println("Add- " + obj.add(9, 3));
  }
}
Output
in add method of the child class
Add- 12

In the example there is a Class A with a method add() which is overridden in the sub class B. When add() method is invoked on an object of type B then the add() method of the class B is called as it overrides the add() method of the parent class A.

Using super to call parent class method

You can use super to call the parent class version of the overridden method.

class A {
  public void display(String msg) {
    System.out.println("in display method " + msg);
  }
}

public class B extends A{
  // Overriding method of the parent class
  public void display(String msg) {
    // calling parent class method
    super.display("Parent class method");
    System.out.println("in display method " + msg);
  }	

  public static void main(String[] args) {
    B obj = new B();
    obj.display("Child class method");
  }
}
Output
in display method Parent class method
in display method Child class method

Rules for method overriding in Java

  1. When the methods have the same name and the signature is also same in both the super and sub classes methods then only it is considered as method overriding. If methods have the same name but the arguments are not same then these are considered as overloaded methods and that becomes method overloading.
  2. Return type of the overridden method in the sub class should be same or the sub type of the return type of the super class method. That is known as the covariant return type in Java.
  3. The visibility of the overridden method can’t be reduced. For example if a method is public in super class the overridden method in the child class can’t be protected or default as that would mean reducing the visibility. On the other hand if method is protected in super class then it can be public in the overridden method.
  4. If method in the super class throws an exception then the subclass overridden method can declare the same exception as declared in the superclass method or subclass can declare the subtype exception of the exception declared in the superclass method.
  5. A method declared final in the super class can’t be overridden in the subclass.

Method overriding in Java and run time polymorphism

Run time polymorphism in Java is achieved through method overriding. Which of the overridden methods has to be called is resolved at run time rather than at compile time. This process is also known as dynamic method dispatch in Java.

A super class reference can also refer to the sub class object, which of the overridden method has to be called depends upon the reference currently held by the super class reference and that decision is made at the run time.

Method overriding in Java – Run time polymorphism example

Let’s try to clarify it with an example. In the example there is a super class Payment with a method doPayment(double amount).

This class is extended by three sub classes and all of them implement the method doPayment.

public class Payment {
  void doPayment(double amount) {
    System.out.println("In parent class payment method");
  }
}
// childclass-1
class CashPayment extends Payment {
  @Override
  public void doPayment(double amount) {
    System.out.println("In child class CashPayment- " + amount);
  }
}
//childclass-2
class CCPayment extends Payment {
  @Override
  public void doPayment(double amount) {
    System.out.println("In child class CCPayment- " + amount);
  }
}
//childclass-3
class MobilePayment extends Payment {
  @Override
  public void doPayment(double amount) {
    System.out.println("In child class MobilePayment- " + amount);
  }
}
public class MainClass {
  public static void main(String[] args) {
    // super class reference
    Payment payment;
    // holding CashPayment instance
    payment = new CashPayment();
    payment.doPayment(56.25);
    // holding CCPayment instance
    payment = new CCPayment();
    payment.doPayment(67.89);
    // holding MobilePayment instance
    payment = new MobilePayment();
    payment.doPayment(34);
    // holding super class Payment instance
    payment = new Payment();
    payment.doPayment(116.78);
  }
}
Output
In child class CashPayment- 56.25
In child class CCPayment- 67.89
In child class MobilePayment- 34.0
In parent class payment method

In the MainClass a Super class reference variable is declared which refers to different sub class instances at different times and make the method call. Method doPayment() of that child class is called which is referred by the super class reference at that time.

That's all for the topic Method Overriding in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like

No comments:

Post a Comment