• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Most Specific Method Invocation

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across the following 2 questions in Dan's mock exam(http://www.danchisholm.net) on 'Type Conversions' topic.
Question 1
--------------------------------------------
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) {
C c = new C();
m(c, c);
}
}

What is the result of attempting to compile and run the above program?
a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Compiler error.
f. Runtime error.
g. None of the Above
Answer is (d) Prints: BB
-------------------------------------------------
Question 4
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");}
static void m(A x, C y) {System.out.print("AC");}
public static void main(String[] args) {
C c = new C();
m(c, c);
}
}

What is the result of attempting to compile and run the above program?
a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Prints: AC
f. Compiler error.
g. Runtime error.
h. None of the Above
Answer is (f) Compiler error.
------------------------------------------------
After reading Dan's explanation for Q1, I thought I understood how most specific method is invoked. But after I did the second question I am confused. Can somebody explain me how the most specific method is chosen?
This is Dan's explanation for Q2:
-------------------------------------
"Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. 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. End of quote. In this case, no method is clearly more specific than all of the others. "
Thanks a lot,
Goutham.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting ..
I tried compiling both the code and as you said the first code compiled and printed BB.
But the second one failed to compile with the error
reference to m is ambiguous, both method m(B,B) in C and method m(A,C) in C match
Which to me sounds true if i pass C as input parameters.
However i am would like someone to explain what does this mean

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.


Thanks
Bishal
 
Bishal P
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, i got it.. During any method invocation there can be more than one method suitable for handling the call. In our example, it is m(B,B) and m(A,C).
So now the task to find the most specific method is taken up by compiler.Doing the JLS test,
Can every call to m(A,C) be passed on to m(B,B) without compilation error? :roll:
Nope.
Can every call to m(B,B) be passed on to m(A,C)?
No again.
Hence there is no suitable method matching the invocation. And we get the compilation error.
Interestingly enough, if you add another method m(C,C) or even m(B,C) it will compile because going by the JLS informal intuition, the most specific method can be found.
If i understood this incorrectly, please let me know.
 
Goutham Km
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bishal,


Can every call to m(A,C) be passed on to m(B,B) without compilation error?
Nope.
Can every call to m(B,B) be passed on to m(A,C)?
No again.


You are right. I think now I understand it better. I found another easy way of resolving it.
In the above question, if you look at the three classes A, B, and C, C is more specific than B and B is more specific than A. Applying this simple rule to each parameter in a method declaration, we can find the most suitable method easily.
If we look at m(A, C) and m(B,B). Parameter 2 in m(A,C) which is C is more specific than parameter 2 in m(B,B). However parameter1 A in m(A,C) is less specific than parameter1 B in m(B,B). So this leads to a compile time error.
If we add m(B,C) then it clearly becomes more specifc applying the above rule.
I think when we have more parameters applying this rule is easier to resolve method invocation.
Please correct me if I am wrong.
Goutham.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I think it's easier if we draw a tree showing which method is more 'specific.'

Methods lower in the tree are more specific than those above it. Btw, sorry if it doesn't look that good, I'm no ASCII artist.
Anyway, from the tree, you can't tell which of the two methods ( m(A,C) or m(B,B) ) is more specific, so the compiler won't know which method to call in method(C,C).
If you add the method m(B,C) to class C, then no compiler error will result, since m(B,C) is more specific than either m(A,C) and m(B,B).
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys:
I have found NO_OF_UP_CASTS technique to be very helpfull in determining the most specific method. So for a given hierarchy say A -> B -> C, to up-cast C to B is 1 cast while C to A is 2 casts.
So if method call is (C,C) and available method is (A,A), so the casts needed will be (2,2) that is total of 4 casts.
Now take Q1:
From method call (C,C) to (A,A) is (2,2)= 4 casts
From method call (C,C) to (A,B) is (2,1)= 3 casts
From method call (C,C) to (B,A) is (1,2)= 3 casts
From method call (C,C) to (B,B) is (1,1)= 2 casts
There is clear winner i.e. (B,B) with minimum casts.
Now take Q2:
From method call (C,C) to (A,A) is (2,2)= 4 casts
From method call (C,C) to (A,B) is (2,1)= 3 casts
From method call (C,C) to (B,A) is (1,2)= 3 casts
From method call (C,C) to (B,B) is (1,1)= 2 casts
From method call (C,C) to (A,C) is (2,0)= 2 casts
Now we have tie between (B,B) and (A,C), so we get ambiguity error...
The only thing to make sure is that cast is automatic such as from derived class to super class (as the case is in above) or for example from int to long which is implicit and legal. Another condition is that all candidates methods are declared in same class. If one them was inherited (say for example (B,B)) was inherited, then it will get more penalty i.e. (1,1) + 1 = 3.
Let me know you comments.
[ September 11, 2002: Message edited by: Barkat Mardhani ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ron, Dan, Marcus, Paul, Val and all other gurus:
The determination of most specific method is very tricky business. I put together a more concrete way of determining it by counting no. of up casts needed. Though it is poorly presented above, it must be sufficient for you gurus to catch on. I have done my own verification. I found it accurate. Can you guys suggest any improvements?
Thanks
Barkat
 
Bishal P
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You guys are cool.. i liked both examples by paul and barkat... specially Barkat's technique is very easy and if proven correct will clear out the confusion in finding the most specific method.
Thanks
Bishal
reply
    Bookmark Topic Watch Topic
  • New Topic