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. Parameters differ in types.
  2. Parameters differ in number.
  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 W3spoint
 */
public class AddExample {
      /**
       * This method is used to add two integer values.
       * @param var1
       * @param var2
       * @author W3spoint
       */
      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 W3spoint
       */
      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 W3spoint
       */
      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.

Download this example.

 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 W3spoint
 */
public class AddExample {
      /**
       * This method is used to add two integer values.
       * @param num1
       * @param num2
       * @author W3spoint
       */
      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 W3spoint
       */
      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

Download this example.

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 W3spoint
 */
public class AddExample {
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @author W3spoint
       */
      void add(int num1, double num2){
            System.out.println(num1 + num2);
      }  
 
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @author W3spoint
       */
      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

Download this example.

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 W3spoint
 */
public class AmbiguityInOverloading {
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @return int
       * @author W3spoint
       */
      int add(int num1, int num2){
            return num1+num2;
      }     
 
      /**
       * This method is used to add two numerics.
       * @param num1
       * @param num2
       * @return float
       * @author W3spoint
       */
      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.w3spoint.business.AmbiguityInOverloading.main
(AmbiguityInOverloading.java:34)

Download this example.

Note: main method can also be overloaded.

MainMethodOverloding.java

/**
 * This class is used for main method overloading.
 * @author W3spoint
 */
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

Download this example.

Interview Questions on Method Overloading and Method Overriding

Next Topic: Method overriding in java with example.
Previous Topic: Polymorphism in java.

 

Content Protection by DMCA.com
Please Share