Difference between Method Overloading and Method Overriding in Java

Method Overloading:
Method Overloading is a Compile time polymorphism. In method overloading, more than one method shares the same method name with different signature in the class. In method overloading, return type can or can not be be same, but we must have to change the parameter because in java, we can not achieve the method overloading by changing only the return type of the method.
Example of method overloading:

class MethodOverloadingEx{  
   static int add(int a, int b){return a+b;}  
   static int add(int a, int b, int c){return a+b+c;}  
 
    public static void main(String args[]) {
      System.out.println(add(4, 6));
      System.out.println(add(4, 6, 7));
    }
} 

Output:

//Here, add with two parameter method runs
10 

// While here, add with three parameter method runs 
17 

Method Overriding:
Method Overriding is a Run time polymorphism. In method overriding, derived class provides the specific implementation of the method that is already provided by the base class or parent class. In method overriding, return type must be same or co-variant (return type may vary in same direction as the derived class).



Example of method overriding:

class Animal{  
      void eat(){System.out.println("eating.");}  
      }  
    class Dog extends Animal{  
    void eat(){System.out.println("Dog is eating.");}  
     }  
   class MethodOverridingEx{  
    public static void main(String args[]) {
      Dog d1=new Dog();
      Animal a1=new Animal();
      d1.eat();
      a1.eat();
    }
} 

Output:

// Derived class method eat() runs
Dog is eating

// Base class method eat() runs
eating 

/* Here, we can see that a method eat() has overridden in the derived class name Dog that is already provided by the base class name Animal.
When we create the instance of class Dog and call the eat() method, we see that only derived class eat() method run instead of base class method eat() and When we create the instance of class Animal and call the eat() method, we see that only base class eat() method run instead of derived class method eat().

So, its clear that in method overriding, method is bound to the instances on the run time which is decided by the JVM. That’s why it is called Run time polymorphism */



Difference between Method Overloading and Method Overriding in Java:

S.NO Method Overloading Method Overriding
1. Method overloading is a compile time polymorphism. Method overriding is a run time polymorphism.
2. It help to rise the readability of the program. While it is used to grant the specific implementation of the method which is already provided by its parent class or super class.
3. It is occur within the class. While it is performed in two classes with inheritance relationship.
4. Method overloading may or may not require inheritance. While method overriding always needs inheritance.
5. In this, methods must have same name and different signature. While in this, methods must have same name and same signature.
6. In method overloading, return type can or can not be be same, but we must have to change the parameter. While in this, return type must be same or co-variant.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.