[Advanced-java] OOP's Question

Bob V bob.v.1974 at gmail.com
Mon Apr 30 02:06:10 PDT 2007


Let us for a moment remove the overrided version of methodA() in A. Then I
think the methodA() in C will be invoked or a runtime exception.
Subsequently if we "revive" the methodA() implementation in A, I think the
methodA() in A will be invoked. Although the invoking object in both the
runs was object of B. More than the Polymorphic behaviour in this case of
overriding, I think the JVM has some kind data structure that indicates what
method to invoke.

And also as a corollary, in this case the methodA() of C will be called only
if super.methodA() is used in B else the methodA of A will be invoked. Isn't
there loss of transparency in this behaviour. The runtime ought to decide by
itself which object to invoke.

What if there are multiple levels of hierarchy and in every layer the
methodA is overrided. How does the JVM decide what method to invoke?

-B

----------------------------------------------------------------------------------------------------------------

The answer is simple and lies in the very definition of "polymorphism".
Choosing the right behavior of an object.

Keep aside the classes for a moment and look at what "object" is the
function being called upon? Whether it is through super's method or its
own, the object on which function is being called would choose the "right"
function.

When you call super.somefunc() and it calls methodA(), the method call is
"still" going to an object of class A, which would eventually choose
overridden method defined in class A. Think in term of objects at
"runtime".

Prashant Parashar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /lists/advanced-java/attachments/20070430/77481867/attachment.html 


More information about the Advanced-java mailing list