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.pr
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.
}
}
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.
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);
}
}
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.
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).