[Advanced-java] OOP's Question
Don Bate
don.bate at bateconsulting.com
Mon Apr 30 09:04:50 PDT 2007
Bob,
There are a few statements in your posting that are
inaccurate. In your second sentence the answer is that the methodA()
in C will be invoked. The fourth sentence is wrong. In both cases the
object is of class A. The implementer of A chose to use some of the
implementation of B and/or C to implement the complete behavior of A.
That is inheritance. The runtime does have a dispatch table that is
built when the constructor is invoked.
The use of super is not all that common except for
constructors. The main other real usage that I see is when the
implementation of methodA in A might invoke super.methodA because
it's augmenting what the parent classes' methods are doing in their
implementations. So generally, the runtime default of using the
most-derived method is correct and is only overridden when an
implementer needs more explicit control. Java is actually a more
simplified solution than the C++ usage of virtual and non-virtual
methods.
Inheritance vs. polymorphism. These are two distinct concepts
that are often confused. Of the two, polymorphism is actually the
more valuable concept. Implementation inheritance is actually a form
of coupling and can cause maintenance issues. The complete
implementation of an object A is actually defined in classes A, B,
and C. A change in the implementation of B or C is also a change in
the complete implementation of an object of class A. The use of super
is actually an increase in coupling and a dependency of the
implementation of A on C. The original example is a suspicious usage
of super in that it is B that is making the decision to use C's
implementtion and not A. Polymorphism is what the classes outside of
A, B, or C can see. They can treat it as a C regardless of whether it
is actually an A, B, or C instance. Polymorphism reduces coupling.
The last question is simple. The JVM uses the most-derived
implementation of the object instance to determine the method to
invoke. super overrides that default to one above the current
derivation. Note that this is determined by the instance:
B obj = new A();
will use A's methodA() if obj.methodA () is invoked, while
B obj = new B();
will use C's methodA() if obj.methodA() is invoked.
At 2:36 PM +0530 4/30/07, Bob V wrote:
>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
>
>
>_______________________________________________
>Advanced-java mailing list
>Advanced-java at lists.XCF.Berkeley.EDU
>https://lists.XCF.Berkeley.EDU/mailman/listinfo/advanced-java
--
Don Bate | Specializing in Consulting and Mentoring in
Bate Consulting, Inc | Object-Oriented Technologies,
| Software Architecture, and Software Process
(972) 618-0208 voice
(972) 618-0216 fax
don.bate at bateconsulting.com
More information about the Advanced-java
mailing list