Method Overloading in Java

Overloading occurs in Java when two or more methods have the same name and different parameter (number and, or type)

For overloading to occur, the following preconditions must be met:

  1. The method names must be same. (remember that Java is case sensitive, as such: getBalance() and getbalance() are two different methods.
  2. The parameter list must vary by number (and, or) type

We see an example of method overloading in the ArithmeticOperation Example below.

 public class ArithmeticOperation {

  public static int sum(int a, int b) {
    return a + b;
  }
  public static float sum(float a, float b) {
    return a + b;
  }
  public static int sum(int a, int b, int c) {
    return a + b + c;
  }
}

In lines 3, 6 and 9, we have three overloaded sum methods, each taking different parameters.

Happy Learning!!!.