Method Overriding in Java

When in inheritance, parent and subclass contains same method but different implementation is called method overriding.

In other word, both parent and subclass share the same method name but different method implementation.

Look at the below example:


class Fruits { //parent class
  public void taste(){
    System.out.println("Fruits are usually tasty!");
  }
}

class Apple extends Fruits{
  //override the taste method
  public void taste(){
    System.out.println("Apple is little bit salty.");
  }
}

class Kiwi extends Fruits{
  //override the taste method
  public void taste(){
    System.out.println("Kiwi is sweet.");
  }
}

class Orange extends Fruits{
  //override the taste method
  public void taste(){
    System.out.println("Oranges are sour overall.");
  }
}

class Carrots extends Fruits{
  //override the taste method
  public void taste(){
    System.out.println("The taste of carrots are little bit bitter.");
  }
}

public class Main{ //must be file name
  
  public static void main (String[] args) {
    //create objects of fruits and subclasses
    
    Fruits fruits = new Fruits();
    Apple apple = new Apple();
    Kiwi kiwi = new Kiwi();
    Orange orange = new Orange();
    Carrots carrots = new Carrots();
    
    //invoking the method
    fruits.taste(); //parent class
    
    apple.taste(); //subclass
    orange.taste(); //subclass 
    kiwi.taste();
    carrots.taste(); // subclass 
  }
}

Output:

Fruits are usually tasty!
Apple is little bit salty.
Oranges are sour overall.
Kiwi is sweet.
The taste of carrots are little bit bitter.

You can see in the above example that, we use the same method among the sub classes but with different implementation. It's called method overriding.

Another real life example could be Bank object that contains a method called getInterestRate(). We will then use the same method among multiple subclasses.


class Bank {
  public float getInterestRate(){
    return 0.0f;
  }
}

class SBI extends Bank{
  public float getInterestRate(){
    return 7.50f;
  }
}

class TDI extends Bank{
  public float getInterestRate(){
    return 5.75f;
  }
}

class BMO extends Bank{
  public float getInterestRate(){
    return 12.50f;
  }
}

public class Main{
  public static void main (String[] args) {
    //create objects of sub-classes
    SBI sbi = new SBI();
    TDI tdi = new TDI();
    BMO bmo = new BMO();
    
    System.out.println("Rate of Interest of SBI is "+sbi.getInterestRate()+"%");
    System.out.println("Rate of Interest of TDI is "+tdi.getInterestRate()+"%");
    System.out.println("Rate of Interest of BMO "+bmo.getInterestRate()+"%");
  }
}

The following output you will get if you run the above example:

Rate of Interest of SBI is 7.5%
Rate of Interest of TDI is 5.75%
Rate of Interest of BMO 12.5%

Benefits of using method overriding?

=> Reuse codes many times among related classes.
=> Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
=> Method overriding is used for runtime polymorphism