12. Method overloading in Java


Encapsulation and Polymorphism In Java: Previous                            Next: Method Overriding 

Method overloading in Java

Method overloading is the way of implementing static/compile time polymorphism in java. Method overloading means more than one methods in a class with same name but different parameters. Parameters can be differing in types, numbers or order. Compiler resolve method call by matching 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. 1. Parameters differ in types.
  2. 2. Parameters differ in number.
  3. 3. Parameters differ in order.

1. Parameters differ in types.

Example:

Below example have two methods which have the same name bur method parameters are differ in their order.
AddExample.java
/**
 * This class is used for method overloading
 * by parameters change in their types.
 * @author java tutorial point
 */
public class AddExample {
      /**
       * This method is used to add two integer values.
       * @param var1
       * @param var2
       * @author java tutorial point
       */
      void add(int var1, int var2){
            System.out.println(var1 + var2);
      }     
 
      /**
       * This method is used to add one double and one integer.
       * @param var1
       * @param var2
       * @author java tutorial point
       */
      void add(double var1, int var2){
            System.out.println(var1 + var2);
      }     
 
      /**
       * This method is used to concatenate two string values.
       * @param var1
       * @param var2
       * @author javatpoint
       */
      void add(String var1, String var2){
            System.out.println(var1 + var2);
      }     
 
      public static void main(String args[]){
            //creating object here
            AddExample addExample = new AddExample();
            //method call
            addExample.add(10, 20);
            addExample.add(12.50, 30);
            addExample.add("hello ", "java.");
      }
}

Output:

 30
 42.5
 hello java.

 2. Parameters differ in number.

Example:

Below example have two methods which have the same name but method parameters are differ in number.
AddExample.java
/**
 * This class is used for method overloading
 * by parameters change in number.
 * @author java tutorial point
 */
public class AddExample {
      /**
       * This method is used to add two integer values.
       * @param num1
       * @param num2
       * @author java tutorial point
       */
      void add(int num1, int num2){
            System.out.println(num1 + num2);
      }     
 
      /**
       * This method is used to add three integer values.
       * @param num1
       * @param num2
       * @param num3
       * @author java tutorial point
       */
      void add(int num1, int num2, int num3){
            System.out.println(num1 + num2 + num3);
      }           
 
      public static void main(String args[]){
            //creating object here
            AddExample addExample = new AddExample();
            //method call
            addExample.add(10, 20);
            addExample.add(20, 30, 40);
      }
}

Output:

30
90

 3. Parameters differ in order.

Example:

Below example have two methods which have the same name bur method parameters are differ in their order.
AddExample.java
/**
 * This class is used for method overloading
 * by parameters change in order.
 * @author java tutorial point
 */
public class AddExample {
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @author java tutorial point
       */
      void add(int num1, double num2){
            System.out.println(num1 + num2);
      }  
 
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @author java tutorial point
       */
      void add(double num1, int num2){
            System.out.println(num1 + num2);
      }           
 
      public static void main(String args[]){
            //creating object here
            AddExample addExample = new AddExample();
            //method call
            addExample.add(10, 20.40);
            addExample.add(20.50, 30);
      }
}

Output:

30.4
50.5

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

Method overloading is not possible by changing return type of the method. Because, as discussed above compiler resolve method call by matching method signature(method name and parameters). If method signatures are same for two or more methods then how compiler will know which method have to be called.

AmbiguityInOverloading.java
/**
 * This class is used to show ambiguity problem
 * in case of change in return type of the method.
 * @author java tutorial point
 */
public class AmbiguityInOverloading {
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @return int
       * @author java tutorial point
       */
      int add(int num1, int num2){
            return num1+num2;
      }     
 
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @return float
       * @author javatutorialpoint
       */
      float add(int num1, int num2){
            return num1 + num2;
      }           
 
      public static void main(String args[]){
      //creating object here
      AmbiguityInOverloading obj = new AmbiguityInOverloading();
      //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 AmbiguityInOverloading
at com.javawithease.business.AmbiguityInOverloading.main
(AmbiguityInOverloading.java:34)

Note: main method can also be overloaded.

MainMethodOverloding.java
/**
 * This class is used for main method overloading.
 * @author javatutorialpoint
 */
public class MainMethodOverloding {
      //overloaded main method with one parm
      public static void main(int num1){
            System.out.println(num1);    
      }
 
      //overloaded main method with two parm
      public static void main(int num1, int num2){
            System.out.println(num1 + num2);
      }
 
      public static void main(String args[]){
            //method call
            main(20);
            main(10, 20);
      }
}

Output:

20
30

Encapsulation and Polymorphism In Java: Previous                            Next: Method Overriding