Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Most Specific Method Invocation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Most Specific Method Invocation" Watch "Most Specific Method Invocation" New topic
Author

Most Specific Method Invocation

Goutham Km
Greenhorn

Joined: Mar 08, 2002
Posts: 5
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.
Bishal P
Ranch Hand

Joined: Sep 06, 2002
Posts: 43
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


_ _____ _ <br />Used to be a Java Programmer but now I work on Microsoft Technologies - Word, Excel and Outlook!
Bishal P
Ranch Hand

Joined: Sep 06, 2002
Posts: 43
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

Joined: Mar 08, 2002
Posts: 5
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.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
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).
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Aug 05, 2002
Posts: 787
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

Joined: Sep 06, 2002
Posts: 43
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Most Specific Method Invocation
 
Similar Threads
the most specific method is chosen rule (?)
Doubt in polymorphism
Overloading ambiguity
Overloading question
method conversion question