class A {
public void printFirst(int... va) throws IOException{
System.out.print("A");
}
public static void main(String args[]) {
try {
new B().printFirst(2);
} catch (Exception ex) {
}
}
}
class B extends A {
//@Override
public void printFirst(float... va) throws IOException{
System.out.print("B");
}
}
Why, it is showing reference to call ambiguous ??
It actually compiles if you remove the varargs notation. The literal
2should be considered anint, not afloat, so I would expect that the printFirst in A would be chosen by the compiler.It looks like this has to do with how the compiler does method invocation conversions. This SO question says it’s in the spec, but the part of accepted answer that relates to this question appears to be contradictory (it says you can’t combine a widening conversion (int to float) with varargs, but then later it says this is okay). A similar problem was discussed in this question and the accepted answer concludes that this case is actually unspecified (unfortunately the link to the discussion is now broken). Making matters worse, the language guide simply suggests avoiding this type of overloading.