Posts

Showing posts with the label ambiguous

How does Java handle potentially ambiguous method calls?

How does Java handle potentially ambiguous method calls? Playing around with ambiguous methods calls that have multiple parameters, I noticed that quite often it was not actually ambiguous when I expected it to be, and this led to some strange behavior that I couldn't quite understand. For example, with the following inheritance structure: public static class A { } public static class B extends A { } public static class C extends B { } And running the method test() . test() public static void test() { test(new C(), new C(), new C()); } For some reason these two methods are ambiguous public static void test(A x, A xx, B xxx) { System.out.println("TEST 1"); } public static void test(A x, C xx, A xxx) { System.out.println("TEST 2"); } However swapping the last two arguments in the second method makes that one take priority. public static void test(A x, A xx, B xxx) { System.out.println("TEST 1"); } public static void test(A x, A xx, C xxx)...