Java
October 14, 2020

Theory: Overloading

Overloading allows you to change the method’s signature: the number of parameters, their type or both. If methods have the same name, but a different number or type of parameters, they are overloaded. It means you can invoke different methods by the same name by passing different arguments.

How to overload methods

Important that it's impossible to declare more than one method with the same name and parameters (number and types), even with different return types. The return type is not considered for overloading because it's not a part of the signature.

Note, that in the case when parameters have different types, changing the order of these parameters is a valid case of overloading.

The overloading mechanism allows us not to write different names for methods that perform similar operations.

Looking ahead, we'll assume that overloading is a form of the static (compile-time) polymorphism.

Overloading and casting

To understand how overloading deals with type casting, let's consider an example of overloaded methods that only differ in the type on the single argument and see when each of them will be invoked and why.

Now if we call print(100), the program outputs:

int arg: 100

What we see here is that 100 is treated as int and the corresponding method is invoked.

In the case where the type of a method parameter is not exactly the same as the type of the passed argument, the compiler chooses the method that has the closest type of the argument in order of the implicit casting.

Since all integer literals are treated as int by default, int will be the starting point. The closest one will then be long.

Let's remove or comment the method public static void print(int a), then recompile and run the program again. The result is as expected:

long arg: 100

Ok, now, let's remove the method public static void print(long a) too. Since we have no method with float argument, the next type in the order of implicit type casting will be double. After recompiling the program outputs:

double arg: 100.0

If we remove the method public static void print(double a) the only method we have left is the one with short type of argument. The program won't compile if we just call print(100) as we did before.

Let's explain why. When we pass some value to the method, the compiler does not evaluate it. All that is known is that it is integer literal and hence has integer type.

In our case, since 100 is treated as an int by default and JVM doesn't know if the passed value can be cast to short safely, the only way to pass short argument is by casting the value explicitly:

Conclusion

Method overloading allows you to implement two or more methods with the same name, but different arguments. The arguments of such methods may differ in their number or type. This helps to avoid having various method references for similar tasks. When invoked, the proper method is chosen based on the provided arguments. If the argument has a different type from what is expected, the closest type of the argument in order of the implicit casting is used.