Reputation: 1827
In other words, what are the precise rules for how the Java compiler determines which overloaded method to choose to execute? I've spent a good amount of time googling and I think I'm not using the right search keywords.
public class C1 extends C2 {}
public class C2 extends C3 {}
public class C3 {}
public class Test {
public static void main(String[] args) {
C1 c1 = new C1();
// What are the precise rules for determining
// which method below is called?
method(c1, c1);
}
static public void method(C3 test, C3 test2) {
System.out.println("C3");
}
static public void method(C2 test, C3 test2) {
System.out.println("C2");
}
}
Upvotes: 2
Views: 78
Reputation: 719259
The relevant part of the JLS is 15.12.2 Compile-Time Step 2: Determine Method Signature. The rules are complicated and technical, but the general principle is that the applicable method with the most specific argument types is chosen.
Upvotes: 1
Reputation: 23960
I think that is stated in the Java Language Spec, Conversions.
I can see from there that is must be a widening reference conversion, but I don't see any remarks about the path. It seems to me it searches for the shortest matching path (in the inheritance tree), as that is most logical, but again, I can't find that in the spec.
Upvotes: 0