«Back to Home

Core Java

Topics

Difference Between Method Overloading And Method Overriding In Java

Differentiation between method overloading and method overriding

In Java, there are many differences between the method overloading and method overriding, which are given below,

 Method Overloading  Method Overriding
Method overloading is mainly used to increase the readability of the program in Java. Method overriding is mainly used to provide the specific implementation of the method, which is already provided by its parent class.
Method overloading is achieved within a class Method overriding occurs between two classes, which have IS-A (inheritance) relationship.
In method overloading, parameter must be different. In method overriding, parameter must be same.
In Java, method overloading is the example of the compile time polymorphism. In Java, method overriding is the example of run time polymorphism.
In Java, method overloading can't be achieved by changing return type of the method only. Return type can be same or different in method overloading but parameter must be changed. Return type should be same or covariant in method overriding.
 
Example-
 
class TestExample{
    static int display(int x,int y){
       return x+y;
     }
static int display(int x, int y, int z){
       return x+y+z;}
}
 
Example-
 
class Vehicle{
      void run(){
          System.out.println(“ Run fast “);}
       }
class Bike extends Vehicle{
      void run(){
          System.out.println(“ Run slow “);}
}

Summary

Thus, we learnt that method overloading is mainly used to increase the readability of the program and method overriding is mainly used to provide the specific implementation of the method, which is already provided by its parent class in Java.