Method Overloading in Java
Method overloading is a concept that allows to declare multiple methods with same name but different parameters in the same class.
Java supports method overloading and always occur in the same class(unlike method overriding).
Method overloading is one of the ways through which java supports polymorphism. Polymorphism is a concept of object oriented programming that deal with multiple forms. We will cover polymorphism in separate topics later on.
Method overloading can be done by changing number of arguments or by changing the data type of arguments.
If two or more method have same name and same parameter list but differs in return type can not be overloaded.
Note: Overloaded method can have different access modifiers and it does not have any significance in method overloading.
There are two different ways of method overloading.
- Different datatype of arguments
- Different number of arguments
Method overloading by changing data type of arguments.
Example:
In this example, we have two sum() methods that take integer and float type arguments respectively.
Notice that in the same class we have two methods with same name but different types of parameters
Sum is 13 Sum is 8.4
You can see that sum() method is overloaded two times. The first takes two integer arguments, the second takes two float arguments.
Method overloading by changing no. of argument.
Example:
In this example, we have two methods
Result is 40 Result is 48
In this example the multiply()
method is overloaded twice. The first method takes two arguments and the second method takes three arguments.
When an overloaded method is called Java look for match between the arguments to call the method and the its parameters. This match need not always be exact, sometime when exact match is not found, Java automatic type conversion plays a vital role.
Example of Method overloading with type promotion.
Java supports automatic type promotion, like int to long or float to double etc. In this example we are doing same and calling a function that takes one integer and second long type argument. At the time of calling we passed integer values and Java treated second argument as long type. See the below example.
Sum is 13
Method overloading if the order of parameters is changed
We can have two methods with same name and parameters but the order of parameters is different.
Example:
In this scenario, method overloading works but internally JVM treat it as method having different type of arguments
Â
Overloading main Method
In Java, we can overload the main() method using different number and types of parameter but the JVM only understand the original main() method.
Example:
In this example, we created three main() methods having different parameter types.
Â
Method overloading and null error
This is a general issue when working with objects, if same name methods having reference type parameters then be careful while passing arguments.
Below is an example in which you will know how a null value can cause an error when methods are overloaded.
Example:
Null value is a default value for all the reference types. It created ambiguity to JVM that reports error.
The method test(Integer) is ambiguous for the type Demo
Reason:
The main reason for getting the compile time error in the above example is that here we have Integer and String as arguments which are not primitive data types in java and this type of argument do not accept any null value. When the null value is passed the compiler gets confused which method is to be selected as both the methods in the above example are accepting null.
However, we can solve this to pass specific reference type rather than value.
Example:
In this example, we are passing specific type argument rather than null value.
Â