Method overloading is an oops concept where two methods have the same name but different parameters.
In method overloading, we can have parameters that can match multiple overloaded methods. In this case, the compiler will select the most specific method and call it. But if there are two specific methods and the compiler cannot decide which method to call, it will throw a compilation error.
In this post, we are going to look at two kinds of ambiguity –
Before getting any further, let’s first look at how a particular overloaded method is selected. The answer is written in the Java documentation itself. According to the documentation, there are mainly three phases to choosing an overloaded method.
- The first phase performs overload resolution without permitting boxing or unboxing conversion or using variable arity method invocation.
- The second phase performs overload resolution while allowing boxing and unboxing but still precludes the use of variable arity method invocation.
- The third phase allows overloading to be combined with variable arity methods, boxing, and unboxing.
So, now below conditions might arise after going through these phases
- An appropriate method is selected and executed
- No method is found according to the method call
- Or more than one overloaded method satisfies the method call
In the last two scenarios, we will get an error.
The null parameter in method overloading
Code Example
package com.codekru.model;
public class AmbiguityMO {
public void method(Object object) {
System.out.println("In object method");
}
public void method(Integer object) {
System.out.println("In integer method");
}
public void method(String object) {
System.out.println("In string method");
}
public static void main(String[] args) {
AmbiguityMO ambiguityMO = new AmbiguityMO();
ambiguityMO.method(null);
}
}
Output –
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method method(Object) is ambiguous for the type AmbiguityMO
Reason
The compiler is confused between the Integer and String parameter type method. Both Integer and String parameter accepts a null value. From null, the compiler is unable to determine its data type.
Solution
- If we remove any integer or string type methods, the compiler will be able to resolve to one of the methods.
- Another solution is to define the Integer or String type variable with a null value. Now the compiler already knows the datatype, so it will be able to resolve to one of the methods.
public static void main(String[] args) {
AmbiguityMO ambiguityMO = new AmbiguityMO();
Integer type = null;
ambiguityMO.method(type); // No error. Will call integer method
}
Ambiguity while using var-args
Scenario 1
Let’s use method overloading on two different types that cannot be converted to one another using autoboxing or unboxing ( like float and boolean ).
public class Codekru {
void method(float... numbers) {
System.out.print("int method called: ");
for (float num : numbers) {
System.out.println(num+" ");
}
}
void method(boolean... values) {
System.out.print("boolean method called: ");
for (boolean value : values) {
System.out.println(value+" ");
}
}
public static void main(String[] args) {
Codekru code = new Codekru();
code.method(1, 2,3);
code.method(false);
code.method(); // this will throw an error
}
}
Output –
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method method(float[]) is ambiguous for the type Codekru
Here we got a compilation error as code.method() satisfies both of the overloaded methods, and the compiler cannot determine which one to call.
Scenario 2
What if we use float arguments with the var-args of the same type as shown below.
void add(float... numbers) {}
void add(float num1, float... numbers){}
Here, add(1,2) method call will also throw a compilation error as it satisfies the condition for both methods and thus again creates an ambiguity.
Thank you for reading this article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.