fuzzy28 fuzzy28 - 4 years ago 93
Java Question

Method overloading : Single parameter vs alternating parameters

This question might be similar to Java overload confusion

I'm reading a book and there's a topic regarding method overloading from which a compiler may not be able to resolve the method being called. The example on the book uses overloaded method with two parameters

(int, double)
and
(double, int)
. Calling this method like overloadedTwoParam(4, 5) will cause a compiler error because int can be passed to double. My question here is why compiler able to resolve which method will be called if my parameter is only one ?

public class Test {

public static void main(String[] args) {
Test t = new Test();
t.overloadedSingleParam(1); //this is working
t.overloadedTwoParam(4, 5); //this is NOT working
}

void overloadedSingleParam(double a) {
// do something here..
}

void overloadedSingleParam(int a) {
// do something here..
}

void overloadedTwoParam(double a, int b) {
// do something here..
}

void overloadedTwoParam(int a, double b) {
// do something here..
}
}

Answer Source

The CKing intuition and answer is good.
I will complete with JLS reference.

The general idea is : the compiler chooses the most specific method ... if it founds it.

The JLS states that the informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. It is the short version.
In the JLS link referenced, all rules applied by the compiler to choose the most specific method are specified.

It is possible that no method is the most specific, because there are two or more methods that are maximally specific. In this case a compilation error occurs specifying that the method invocation is ambiguous.


In the first scenario the compilation is fine as you have a unique maximally specific method :

Test t = new Test();
t.overloadedSingleParam(1);

...
void overloadedSingleParam(double a) {
    // do something here..
}
void overloadedSingleParam(int a) {
    // do something here..
}

void overloadedSingleParam(int a) is an exact matching as we have both int as formal parameter of the method and effective parameter.
While overloadedSingleParam(double a) requires an implicit conversion from int to double.

So overloadedSingleParam(double a) is less specific than void overloadedSingleParam(int a)when an int value is passed as argument.
So the compiler chooses void overloadedSingleParam(int a) and the compilation is successful.


In the second scenario, things are different : you fall in the case of no one method is more specific than another one :

Test t = new Test();
t.overloadedTwoParam(4, 5);

...
void overloadedTwoParam(double a, int b) {
    // do something here..
}

void overloadedTwoParam(int a, double b) {
    // do something here..
}

You can check it with the informal intuition :

  • overloadedTwoParam((double)3, 4) applied to void overloadedTwoParam(int a, double b) doesn't compile.

  • overloadedTwoParam(3, (double)4) applied to void overloadedTwoParam(double a, int b) doesn't compile either.

Consequently a compilation error occurs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download