fastread homefastrread library fastread menu

Java : Difference between method overloading and method overriding in java

Tutorial by:Arena Buocel      Date: 2016-04-30 00:21:37

❰ Previous Next ❱

There are many differences between method overloading and method overriding in java. A list of differences between method overloading and method overriding are given below:

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.

Java Method Overloading example

 
  1. class OverloadingExample{  
  2. static int add(int a,int b){return a+b;}  
  3. static int add(int a,int b,int c){return a+b+c;}  
  4. }  

Java Method Overriding example

 
  1. class Animal{  
  2. void eat(){System.out.println("eating...");}  
  3. }  
  4. class Dog extends Animal{  
  5. void eat(){System.out.println("eating bread...");}  
  6. }  

❰ Previous Next ❱


Java

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website