Method overloading in java

Method overloading is a mechanism to implement static/compile time polymorphism in Java. Method overloading means more than one method in a class with the same name but different parameters. Parameters can differ in type, number, or order. Compiler resolves method calls by matching the method signature at compile time, that’s why it is known as static or compile time polymorphism. It is also known as static binding.

Ways to implement method overloading in Java:

  1. Parameters differ in type.
  2. Parameters differ in number.
  3. Parameters differ in order.

1. Parameters differ in type.

The below example has two methods that have the same name but method parameters differ in their order.

MethodOverloadingTest1.java

package com.w3schools;

public class MethodOverloadingTest {
    
    void add(int var1, int var2){
          System.out.println(var1 + var2);
    }     

    void add(double var1, int var2){
          System.out.println(var1 + var2);
    }     

    void add(String var1, String var2){
          System.out.println(var1 + var2);
    }  

    public static void main(String args[]){
        //Create Object 
    	MethodOverloadingTest obj = new MethodOverloadingTest();
    	
        //method call
    	obj.add(100, 200);
    	obj.add(120.50, 300);
    	obj.add("Hello ", "W3schools360!");
  }
}

Output:

300
420.5
Hello W3schools360!

2. Parameters differ in number.

The below example has two methods that have the same name but method parameters differ in number.

MethodOverloadingTest.java

package com.w3schools;

public class MethodOverloadingTest {
    
   void add(int num1, int num2){
         System.out.println(num1 + num2);
   }     

   void add(int num1, int num2, int num3){
         System.out.println(num1 + num2 + num3);
   }   

    public static void main(String args[]){
        //Create Object 
    	MethodOverloadingTest obj = new MethodOverloadingTest();

        //method call
    	obj.add(100, 200);
    	obj.add(200, 300, 400);
  }
}

Output:

300
900

3. Parameters differ in order.

The below example has two methods that have the same name but method parameters differ in their order.

MethodOverloadingTest.java

package com.w3schools;

public class MethodOverloadingTest {
    
    void add(int num1, double num2){
        System.out.println(num1 + num2); 
    }    

    void add(double num1, int num2){
        System.out.println(num1 + num2); 
    } 

    public static void main(String args[]){
        //Create Object 
    	MethodOverloadingTest obj = new MethodOverloadingTest();

        //method call
    	obj.add(100, 200.40); 
    	obj.add(200.50, 300);
  }
}

Output:

300.4
500.5

Why method overloading is not possible by changing the return type of the method?

It is because, as discussed above compiler resolves the method call by matching the method signature(method name and parameters). If method signatures are the same for two or more methods then how compiler will know which method has to be called.

MethodOverloadingAmbiguity.java

package com.w3schools;
public class MethodOverloadingAmbiguity {

      int add(int num1, int num2){
            return num1+num2;
      }     

      float add(int num1, int num2){
            return num1 + num2;
      }           

      public static void main(String args[]){
    	  //creating object here
    	  MethodOverloadingAmbiguity obj = new MethodOverloadingAmbiguity();
    	  
    	  //compiler can't differentiate method call
    	  System.out.println(obj.add(10, 20));
      }
}

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method add(int, int) is undefined for the type MethodOverloadingAmbiguity
at com.w3schools.MethodOverloadingAmbiguity.main (MethodOverloadingAmbiguity.java:34)

Override the main method in Java

MainMethodOverloding.java

package com.w3schools;
public class MainMethodOverlodingTest {
      
//Main method with one parameter
      public static void main(int num1){
            System.out.println(num1);    
      }

      //Main method with two parameters
      public static void main(int num1, int num2){
            System.out.println(num1 + num2);
      }

      public static void main(String args[]){
            //method call
            main(200);
            main(100, 200);
      }
}

Output:

200
300