Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..
    }
}
like image 839
Ruelos Joel Avatar asked Feb 05 '17 08:02

Ruelos Joel


2 Answers

My question here is why compiler able to resolve which method will be called if my parameter is only one

When it comes to a single parameter, the compiler is able to see that there is a method that takes an int so there is no confusion about which method should be called. Even if you delete the method that takes a single int, the compiler will still be able to call the method that takes a single double since there is no ambiguity here. (int can be promoted to a double)

When it comes to the other methods that alternate the int and double parameters, the compiler does not want to take responsibility for deciding whether to promote the first parameter to a double or the second parameter to a double.

like image 117
Chetan Kinger Avatar answered Nov 15 '22 03:11

Chetan Kinger


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.

like image 2
davidxxx Avatar answered Nov 15 '22 02:11

davidxxx