7
public class aman {
    void m(double a , int b, int c) {
        System.out.println("second");
    }
    void m(float a , int b, double c) {
        System.out.println("first");
    }
    public static void main(String[] args) {
        aman obj = new aman();
        obj.m(23, 12, 1);
    }
}

Here, method m() has been overloaded but i am not understanding why the call is ambigous because in first method only 1 conversion needs to take place whereas in second method, two conversions are required. So, definitely first method should have been called. Please give the reason why this is not happening or if i am missing some rule.

user2672165
  • 2,986
  • 19
  • 27
Aman
  • 979
  • 3
  • 10
  • 23

5 Answers5

11

The JLS will not consider 2 conversions and 1 conversion to be a difference. It will only distinguish between having-to-convert and not-having-to-convert.

Since both methods have-to-convert, they are equally possible.

Related to this subject, there is my answer on a similar question (but not entirely the same).

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
  • thnks for your answer, i have gone through it and also visited http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5 but here i could not understand a line **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.** Can you explain it – Aman Apr 10 '14 at 15:38
  • Definitely. What that sentence basically means is that a method can be considered more specific than another if any call to the first method can also be resolved by the latter method. This would mean that for example a method `m(Object)` can resolve all calls to `m(User)` (since a `User` is an `Object`). Therefore we can say that the method `m(User)` is more specific than the other one because calls to `m(Object)` (like `m(Integer)`) cannot be resolved by `m(User)` because there is no relation between `Integer` and `User`. – Jeroen Vannevel Apr 10 '14 at 15:44
  • so in my case no method is more specific, is that the reason of compile time error ? – Aman Apr 10 '14 at 15:56
  • 1
    Yes, exactly. As the [very last option of JLS $15.12.2.5 "Choosing the most specific method"](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5) says: **Otherwise, we say that the method invocation is ambiguous, and a compile-time error occurs.**. – Jeroen Vannevel Apr 10 '14 at 15:58
  • thnks @Jeroen Vannevel this discussion really added to my concepts – Aman Apr 10 '14 at 16:00
4

here , method will be ambiguous becs you are filling all parameters as integer values, then the compiler will confuse (for automatic type cast). thus you need to define something like this suffix for your code:

public class aman {
    void m(double a , int b, int c) {
        System.out.println("second");
    }
    void m(float a , int b, double c) {
        System.out.println("first");
    }
    public static void main(String[] args) {
        aman obj = new aman();

        obj.m(20d, 30, 40);//for calling first method 
        obj.m(23f, 12, 1d);//for calling second method.
    }
}
Raju Sharma
  • 2,496
  • 3
  • 23
  • 41
0

Here both the promotion is possible ,int to float to double. so compiler is not able to take decision which method is to be called hence gives ambiguous error at runtime . A loop hole in method overriding automatic promotion of type.

-1
public class Test {

    void m(int c , int b, int d) {
        System.out.println("Automatic promotion in overloading--->"+c);

    }

    public static void main(String[] args) {
        Test obj = new Test();
            obj.m('A', 30, 40);
    }
}
Linh
  • 57,942
  • 23
  • 262
  • 279
-2

It is ambigous because you are calling it with three Integer literals.

You have to use either:

obj.m(23d, 12, 1);

or

obj.m(23, 12, 1f);

to bring out wich argument is wanted as is and wich arguement can be casted.

ifloop
  • 8,079
  • 2
  • 26
  • 35
  • yes i know, so what are you trying to say. Please explain in detail – Aman Apr 10 '14 at 14:39
  • @user3519914 I edited my answer, this might help. Otherwise see [Jeroen Vannevel](http://stackoverflow.com/users/1864167/jeroen-vannevel)s answer as well. – ifloop Apr 10 '14 at 14:44