Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More specific???

 
Sun LiWei
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {}
class B extends A {}
class C extends B {
static void m(A x, A y) {System.out.print("AA");}
static void m(A x, B y) {System.out.print("AB");}
static void m(B x, A y) {System.out.print("BA");}
static void m(B x, B y) {System.out.print("BB");}
public static void main(String[] args) {
m(null,null);
}
}
I know that m(null,null) calls m(B x,B,y) and my question is why m(B x, B y) is more specific than
m(A x, A y)???
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that m(null,null) calls m(B x,B,y) and my question is why m(B x, B y) is more specific than
Because m(B x, B y) has arguments that more derived (specific) than m(A x, A y).
Barkat
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following quote is from theJava Language Specification, Section 15.12.2.2, Choose the Most Specific Method.
The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic