Adding new methods that overload existing method names does not break compatibility with pre-existing binaries. The method signature to be used for each method invocation was determined when these existing binaries were compiled; therefore newly added methods will not be used, even if their signatures are both applicable and more specific than the method signature originally chosen.
While adding a new overloaded method or constructor may cause a compile-time error the next time a class or interface is compiled because there is no method or constructor that is most specific (§15.11.2.2), no such error occurs when a Java program is executed, because no overload resolution is done at execution time.
class Super { static void out(float f) { System.out.println("float"); } } class Test { public static void main(String[] args) { Super.out(2); } }
is compiled and executed, it produces the output:
float
Suppose that a new version of class Super
is produced:
class Super { static void out(float f) { System.out.println("float"); } static void out(int i) { System.out.println("int"); } }
If Super
is recompiled but not Test
, then running the new binary with the existing binary of Test
still produces the output:
float
However, if Test
is then recompiled, using this new Super
, the output is then:
int
as might have been naively expected in the previous case.