Method Overriding in Java

If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java.
In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

Usage of Java Method Overriding

  • Method overriding is used to provide specific implementation of a method that is already provided by its super class.
  • Method overriding is used for runtime polymorphism
  • Rules for Java Method Overriding

  • method must have same name as in the parent class.
  • method must have same parameter as in the parent class.
  • must be IS-A relationship (inheritance).
  • Understand the problem without method overriding

    Example

    class Car {  
    void run() {
    System.out.println("Car is running");
      }  }  
    class Alto extends Car{  
    public static void main(String args[]){  
    Alto obj = new Alto();  
    obj.run();  
     } }       
    

    Output:

    Car is running

    Example of method overriding

    we have defined the run method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method is same and there is IS-A relationship between the classes, so there is method overriding.

    Example:

    File: TestOverloading.java
    class Car {  
    void run() {
    System.out.println("Car is running");
      } }  
    class Alto1  extends Vehicle {  
    void run() {
    System.out.println("Alto  is running with speed");
      }  
    public static void main(String args[]){  
    Alto1  obj = new Alto1 ();  
    obj.run();  
      }  
    

    Output:

    Alto is running with speed

    Real example of Method Overriding

    Consider a scenario, Bank is a class that provides functionality to get rate of interest. But, rate of interest varies according to banks. For example, HDFC, PNB and IDBI banks could provide 4%, 6% and 8% rate of interest.

    Example:

    class Bank {  
    int getRateOfInterest(){
    return 0;
      } }  
    class HDFC extends Bank {  
    int getRateOfInterest() {
    return 4;
      } }  
    class PNB extends Bank{  
    int getRateOfInterest() {
    return 6;
      } }  
    class IDBI extends Bank {  
    int getRateOfInterest() {
    return 8;
      } }  
    class BankTest {  
    public static void main(String args[]) {  
    HDFC h = new HDFC();  
    PNB p = new PNB();  
    IDBI  i = new IDBI ();  
    System.out.println("HDFC Rate of Interest: "+s.getRateOfInterest());  
    System.out.println("PNB Rate of Interest: "+i.getRateOfInterest());  
    System.out.println("IDBI Rate of Interest: "+a.getRateOfInterest());  
     } }     
    

    Output:

    HDFC Rate of Interest: 4
    PNB Rate of Interest:  6
    IDBI Rate of Interest: 8
    

    Can we override static method?

    No, static method cannot be overridden. It can be proved by runtime polymorphism.

    Why we cannot override static method?

    because static method is bound with class whereas instance method is bound with object. Static belongs to class area and instance belongs to heap area.

    Can we override java main method?

    No, because main is a static method.

    Difference between method overloading and method overriding


    No.Method Overloading Method Overriding
    1.

    Method overloading is used to increase the readability of the program.

    Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
    2.
    Method overloading is performed within class.
    Method overriding occurs in two classes that have IS-A (inheritance) relationship.
    3.
    In case of method overloading, parameter must be different.
    In case of method overriding, parameter must be same.
    4.Method overloading is the example of compile time polymorphism.Method overriding is the example of run time polymorphism.
    5.In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.Return type must be same or covariant in method overriding.