Method Overloading in Java

Method overloading in java allows us to have multiple methods with same name but with different parameters (different number of parameters, different types of parameters, or both). It is similar to constructor overloading in Java, that allows us to have more than one constructor with different parameter lists.

We cannot overload methods based on different return types. It is because method overloading is not associated with return types. Overloaded methods may have the same or different return types, but they must differ in parameters.

For Example:
int add(int i, int j)
int add(int i, int j, int k)
float add(double i, double j)
float add(int i, int j, double k)

Above mentioned add() methods are overloaded. We have four different versions of add() method, each varies by number or type of parameters.

Method overloading is an example of Static Polymorphism. Method overloading is an example of static binding where compiler decides which version of over loaded method to call based on the number and type of parameters at compile time.


Why do we need method overloading ?

Suppose, you want to perform the addition of either 2 or 3 numbers. You can create two methods add2Numbers(int i, int j) and add3Numbers(int i, int j, int k) for two and three parameters respectively. However, other programmers, as well as you in the future may get confused as the behavior of both methods are the same but they differ by name.

We can use method overloading in this scenario and depending upon the number of argument passed to call one of the overloaded methods. This helps to increase the readability of the program and we don’t have to create and remember different names for functions doing the same thing.


How to perform method overloading in Java

We can implement method overloading in three different ways:

Different numbers of arguments

Here is an example of method overloading by different number of paramaters.

int add(int i, int j)
int add(int i, int j, int k)
public class DifferentParameterCount {
  static int add(int i, int j) {
    System.out.println("inside add(int, int)");
    return i + j;
  }

  static int add(int i, int j, int k) {
    System.out.println("inside add(int, int, int)");
    return i + j + k;
  }

  public static void main(String[] args) {
    System.out.println("2 + 3 = " + add(2, 3));
    System.out.println("2 + 3 + 4 = " + add(2, 3, 4));
  }
}
Output
inside add(int, int)
2 + 3 = 5
inside add(int, int, int)
2 + 3 + 4 = 9

In above program, add() method is overloaded based on the number of parameters. Which version of add() method to call is decided based on the number of int parameters (int, int) or (int, int, int).


Different types of arguments

Here is an example of method overloading by different type of paramaters.

int add(int i, int j)
float add(double i, double j)
public class DifferentParameterType {
  static int add(int i, int j) {
    System.out.println("inside add(int, int)");
    return i + j;
  }

  static double add(double i, double j) {
    System.out.println("inside add(double, double)");
    return i + j;
  }

  public static void main(String[] args) {
    System.out.println("2 + 3 = " + add(2, 3));
    System.out.println("2.5 + 3.1 = " + add(2.5, 3.1));
  }
}
Output
inside add(int, int)
2 + 3 = 5
inside add(double, double)
2.5 + 3.1 = 5.6

In above program, add() method is overloaded based on the data type of parameters. Which version of add() method to call is decided based on the data type of parameters (int, int) or (double, double).


Different sequence of arguments

Here is an example of method overloading by different sequence of paramaters.

double add(int i, double j)
double add(double i, int j)
public class DifferentParameterSequence {
  static double add(int i, double j) {
    System.out.println("inside add(int, double)");
    return i + j;
  }

  static double add(double i, int j) {
    System.out.println("inside add(double, int)");
    return i + j;
  }

  public static void main(String[] args) {
    System.out.println("2 + 3.5 = " + add(2, 3.5));
    System.out.println("2.2 + 3 = " + add(2.2, 3));
  }
}
Output
inside add(int, double)
2 + 3.5 = 5.5
inside add(double, int)
2.2 + 3 = 5.2

In above program, add() method is overloaded based on the sequence of parameters. Which version of add() method to call is decided based on the relative sequence of parameters (int, double) or (double, int)