Method Overloading in Java

When more than one method have same name but with differen types of parameters or the number of parameters are different within the class (not subclass) then it is called method overloading.

“ One big difference between method overriding and method overloading is that, method overloading can be achieved within the class and method overriding can be achieved within the subclass. ”

In both way we reuse the same method but the use cases are totally different.

< Anyway,

Let's check out the below example about method overloading:


public class Addition{
  
  public int add(int x, int y){
    return x+y;
  }
  public int add(int x, int y, int z){
    return x+y+z;
  }
  public float add(float x, float y){
    return x+y;
  }
  
  public static void main (String[] args) {
    Addition obj = new Addition();
    
    System.out.println("Addition of 2 int numbers are "+ obj.add(75, 75));
    System.out.println("Addition of 3 int numbers are "+ obj.add(75, 25, 50));
    System.out.println("Addition of 2 float number s are "+ obj.add(7.5f, 7.5f));
  }
}

Output:
Addition of 2 int numbers are 150
Addition of 3 int numbers are 150
Addition of 2 float number s are 15.0

//Link: Differences between method overloading and method overriding.