The concept of Method Overloading in Java is where a class can have multiple methods with the same name, provided that their argument constructions are different. Method Overloading is comparable to constructor overloading where we can implement multiple constructors (also the same name), provided that these constructors have different argument construction.

Method Overloading in Java

  • Method name should be exactly same. Java is case sensitive, so two methods with name foo()
    and fOO() are totally different and doesn’t come under method overloading in java.
  • The difference between overloaded methods are the arguments. Overloaded methods should have different method arguments. For example, different number of parameters, different data types of parameters etc.
  • Method return type have no significance for overloading, java compiler will complain about duplicate method in the class.
  • Method parameter names have no significance in method overloading. So in terms of java, foo(int index) and foo(int i) are same.
  • Method overloading is also called compile time polymorphism. Read more at OOPS Concepts in Java.

Let’s look into some examples of method overloading in java.

Method Overloading in Java Example

The snippet above shows a classic example of Method Overloading. Two gatherInput() methods are declared under GetAverage Class. In usual cases, java compiler would display the error: Duplicate method gatherInput() in type GetAverage. However, in this case, we see that the two methods possess different argument list, which makes this approach in java permissible.

Instances where Method Overloading is permissible

  1. Parameter Quantity: Method Overloading is allowed within the class given that the number of parameters of the overloaded methods are not the same.
  2. Parameter Data Types: Method Overloading is allowed within the class given that at least one pair of parameters, from each overloaded method, are of different data type.
  3. Order of Parameters: Method Overloading is allowed within the class given that the order of the data type variables are not the same for overloaded methods.

Instance where Method Overloading is Impermissible

  • Method Return Type: Method Overloading is prohibited within the class with methods having identical parameter list but with different method return type. Bear in mind that Overloading focuses on the parameters, not the return type.

    Above code will produce compiler error: Duplicate method gatherInput(int, double) in type GetAverage.
  • method-overloading-in-java

Type Promotion in Java

Type Promotion is a java concept where a data type of smaller rank is promoted to the data type of higher rank.
The figure below illustrates the data type size promotion order:


As can be observed from the above diagram, byte may be promoted to short. Int data type have three possible promotions: double, long, or float.

Type Promotion and Method Overloading in Java

Type Promotion goes hand in hand with Method Overloading in Java. Type Promotion extends the logic of method overloading when it comes to deciding which overloaded method is used, in cases where values passed does not coincide with any of the parameter data types established.

Note: Passed value data type is automatically promoted to its data type next in line (refer to figure above), if no matching data type is found.

Let’s look into some of the examples of type promotion and overloading in java.

    1. Type Promotion Example 1:

      Output: 10 type long

      Two integers are passed in overloaded add() method. Since no overload methods possess (int, int) arguments, the passed values will be promoted to the nearest bigger type i.e. long.

    2. Type Promotion Example 2:

      Output: 10.0 type float

      Here the nearest bigger type for int is float, hence the output.

    3. Type Promotion Example 3:

      Here java compiler will produce error as The method add(double, int) is ambiguous for the type TypePromotionExample. It’s because both methods are applicable and type conversion is required for any one of them to execute, so java compiler can’t decide which one to go for.

    4. overloading-in-java-ambiguous-error

Method Overloading in Java Valid/Invalid Cases

Let’s look at some code snippets for method overloading use case, whether they are valid or not.

  • Method Overloading in Java Example 1

    Compile time error message Duplicate method method(int, int, int). Even if the variable names differ, java only looks into data types being created and in this example, are both identical(int, int, int).
  • Method Overloading in Java Example 2

    Valid case of method overloading. Data types of arguments are different.
  • Method Overloading in Java Example 3

    Valid case of method overloading, number of arguments are different.
  • Method Overloading in Java Example 4

    Valid case of method overloading. Sequence of the data types of parameters are different, first method having (int, float) and second having (float, int).
  • Method Overloading in Java Example 5

    Compile time error because argument lists are identical. Even if return type of methods and variable names are different, it’s not a valid case. Since return type or variable name literals are not criteria for method overloading.

That’s all for method overloading in java.

Reference: Wikipedia

By admin

Leave a Reply