aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes dynamic method lookup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "dynamic method lookup" Watch "dynamic method lookup" New topic
Author

dynamic method lookup

Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Edited comment: I have revised this question. See post at 03:09 PM below.
I get lost trying to follow the dynamic method lookup for this simple example:

Let�s walk through JLS 15.12.4.4 for the method invocation new Q().m().
The invocation mode is virtual. The actual run-time class of the target object is Q (R = Q). Therefore, the dynamic lookup process starts from class Q. (S = Q)
The compile-time type of the target reference of the method invocation is Q. (X = Q)
Step 1. class Q does not contain a declaration for m.
Step 2. S becomes the superclass of Q. S = P.
Step 1. class P contains a declaration for m. The invocation mode is virtual. PROBLEM: Does the declaration in P override Q.m? This does not make sense.
Revised - added extends P. Thank you Brian & Anupam.
Revised again - Changed Step 1 due to Anumpam's comment.
[ June 03, 2003: Message edited by: Marlene Miller ]
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Shouldn't there be an Q extends P??? Aside from that...
I admit, I can't follow the JLS half the time. But do you really need to understand the JLS for something like this?
1.You know that subclasses will inherit non-private methods from the superclass.
2. You then have the option to override the method or not.
3. If you do override, then the code in your subclass method will ALWAYS be called.
4. If you don't override, then the code in the superclass method will always be called.
To see the dynamic binding in action, have a reference of the superclass point to an instance of the subclass:
SuperClass a = new SubClass();
a.m(); // Calls SubClass m() if it is overridden
// Otherwise it calls SuperClass m().
[ June 03, 2003: Message edited by: Brian Joseph ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Marlene
I couldn't follow your question. Did by any chance you wanted to extend class P.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Marlene
In case you wanted to extend then there is there is a m() method in Q which has been inherited from P. At runtime new Q().m(); would cause that inherited method to run. Which is simply the P's m() method.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I would like to know what sentence or part thereof of JLS 15.12.4.4 have I misunderstood, applied incorrectly or neglected to consider? Or where is the error in my logic?
[ June 03, 2003: Message edited by: Marlene Miller ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Please also read the next and the next to next post.
Hi Marlene
I think that a line of your first post should be like this
Orignal line:
The compile-time type of the target reference of the method invocation is Q. (X = Q)
should be
The compile-time type of the target reference of the method invocation is P(X=P).
Then secondly
Step 1. class P contains a declaration for m. The invocation mode is virtual. PROBLEM: The declaration in S (= P) does not override X.m (= Q.m), because there is no Q.m
There is a Q.m which has been inherited from P.
[ June 03, 2003: Message edited by: Anupam Sinha ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Anumpam, Thank you for considering my question.
You say that the compile time type of the target reference is P. Since the method invocation is new Q().m(), the target reference is the value of new Q() and the compile time type is Q.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Marlene
Yes you are correct. I was thinking of something like P p=new Q();. Sorry for that.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Marlene

Does the declaration in P override Q.m? This does not make sense.

I guess that there is no overriding that is taking place here. Only inheritance.
I think that this may be of some help (from JLS 15.12.4.4)
Otherwise, if S has a superclass, this same lookup procedure is performed recursively using the direct superclass of S in place of S; the method to be invoked is the result of the recursive invocation of this lookup procedure.

Secondly as stated in the first post
Step 1. class P contains a declaration for m. The invocation mode is virtual. PROBLEM: Does the declaration in P override Q.m? This does not make sense.

class P does not contain the method declaration for m() as that would be overriding or overloading(which way it is implemented) but it simply inherits the method.
[ June 03, 2003: Message edited by: Anupam Sinha ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is my question, revised:
If you apply the dynamic lookup procedure described in JLS 15.12.4.4 literally as if you were reading code, in this simple example, for the method invocation m(), the procedure will not find the method in P.
class P { void m() }
class Q extends P { void t() { m(); } }
The problem is the declaration in S will never override X.m.
[ June 03, 2003: Message edited by: Marlene Miller ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I wrote an article in the JR newsletter last year in February. It might be of some help.
JLS 15.12 in Plain English


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Valentin for the reference. Alas, the article does not address my question. In Step 7 you say
if the invocation mode is virtual and the method declaration in S overrides the one in the compile-time type of the target reference of the method invocation, then m is the method to be invoked. The method lookup ends here.

I have two examples where the invocation mode is virtual and the method declaration in S does not override the one in the compile-time type of the target reference of the method invocation. The reason is S is *the same as* the one in the compile-time time type of the target reference of the method invocation.

In these two examples, X == Q. First S == Q. Then S == P. In both cases the procedure does not find m.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I think that the quote from JLS given by Anupam is the key.
new Q().m();
The compile type of the expression where the method invocation occurs is Q. There is no such method declared in Q type, but there is an inherited method m that matches the call. That is what method inherititance is all about: the code written in a class can be called on an instance of a subclass. We are not specializing that code for the subclass, because the method was not overriden.
void t() { m() }
in an instance method this is the same as
this.m();
the compile type of this is the class containing the expression where it occurs. Thus the same explanation as above applies.
Hope it helps.


SCJP2. Please Indent your code using UBB Code
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thanks to all for try to help me to understand.
We all have an intuitive understanding of compile-time method resolution and run-time dynamic method lookup. We all know what inheritance and overriding are.
I could be wrong, but I think you are all earnestly giving me explanations based on your intuitive understanding. I don�t think you are actually reading the words and English logic of the JLS exactly as written.
This surprises me, because I know you are all very competent at reading code and programming logic exactly as it is written.
I think if you were to read JLS 15.12.4.4 as if you were reading code, if you set X to Q, S to Q, set the invocation mode to virtual and walk through the words, and follow the recursion as if it were a while loop, you would see that the method m is not found.
Presumably, everyone has read JLS 15.12.4.4 as carefully as they want to and is comfortable with what it says.
Oh well. We all have more urgent things to do. Thank you again.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
After reading the following

# If class S contains a declaration for a non-abstract method named m with the same descriptor (same number of parameters, the same parameter types, and the same return type) required by the method invocation as determined at compile time (�15.12.3), then:
* If the invocation mode is super or interface, then this is the method to be invoked, and the procedure terminates.
* If the invocation mode is virtual, and the declaration in S overrides (�8.4.6.1) X.m, then the method declared in S is the method to be invoked, and the procedure terminates.
# Otherwise, if S has a superclass, this same lookup procedure is performed recursively using the direct superclass of S in place of S; the method to be invoked is the result of the recursive invocation of this lookup procedure.

I agree with you if you say that the literal application of this procedure will not locate m.
The reason:

The problem is the declaration in S will never override X.m.

Either there is an implicit idea of stopping the procedure when a matching method declaration is found; or there is a missing parragraph telling us that the search is finished when the invocation mode is virtual, S is a superclass of X, and of course the method was found in S.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you very much Jose for taking another look at this problem. Thank you for your time.
You have seen what I saw. I appreciate your comments.
My guess is the author was so focused on the case where one method overrides another that he forgot about the cases where there is only one method - either in the class of the target object or in the superclass.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: dynamic method lookup