[Advanced-java] OOP's Question

Prashant Parashar pparashar at csc.com
Sun Apr 29 22:40:57 PDT 2007


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


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is a PRIVATE message. If you are not the intended recipient, please 
delete without copying and kindly advise us by e-mail of the mistake in 
delivery. NOTE: Regardless of content, this e-mail shall not operate to 
bind CSC to any order or other contract unless pursuant to explicit 
written agreement or government initiative expressly permitting the use of 
e-mail for such purpose.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------




Don Bate <don.bate at bateconsulting.com> 
Sent by: advanced-java-bounces at lists.XCF.Berkeley.EDU
04/28/2007 12:44 AM

To
Brian Mulanda <bmulanda at yahoo.com>, "G.N.V. Jayprakash" 
<gnvjp at hotmail.com>, advanced-java at lists.XCF.Berkeley.EDU
cc

Subject
Re: [Advanced-java] OOP's Question






Conceptually, there's a dispatch table that is part of the object 
instance and this table is constructed at object creation time. It's 
is used to determine which implementation of a method to invoke. By 
default, invocations go against the most extended implementation but 
that default can be altered only by using super in Java. Class B has 
visibility to Class C's methodA's signature but not it's 
implementation so it's going to actually invoke whichever 
implemention is in the instance's dispatch table.

Hope this helps.

Don

At 9:42 AM -0700 4/27/07, Brian Mulanda wrote:
>Jayprakash,
>
>I understand why you question this behavior; it's not entirely obvious
>which methodA() would be invoked. At first I thought it would be C's
>methodA(). However, tests prove that it is A's methodA() that get's
>invoked.
>
>Now the question is why. Intuition suggests, it's because the chain of
>method calls leading to the invocation of methodA() originated from A.
>So the JVM looks for methodA() "bottom-up" with A at the bottom and C
>at the top. This may not be the correct explanation.
>
>So, it's worth investigating how method invocation works in such a case.
>When you find out, please share with us your findings.
>
>_____
>Brian
>
>--- "G.N.V. Jayprakash" <gnvjp at hotmail.com> wrote:
>
>>
>>  Hi,
>>       I have a question in Object Oriented Programming:
>> 
>>  A extends B. B extends C. There is a public method methodA() 
>>implemented in C. methodA() is also
>>  overridden in A.
>> 
>>  There is a flow starting from A from preProcessRequest() 
>>implemented in A. preProcessRequest()
>>  is also implemented in B.
>> 
>>  Now preProcessRequest() in A invokes super.preProcessRequest(). 
>>The control goes to the
>>  preProcessRequest() in B.
>>  preProcessRequest() method implementation in B invokes methodA().
>> 
>>  Now the question is: Will the methodA() implemented in C will be 
>>invoked or the methodA()
>>  implemented in A will be invoked? Why?.
>> 
>>  Thanks & Regards,Jayprakash
>>  _________________________________________________________________
>>  News, entertainment and everything you care about at Live.com. Get it 
now!
>>  http://www.live.com/getstarted.aspx> 
>>_______________________________________________
>>  Advanced-java mailing list
>>  Advanced-java at lists.XCF.Berkeley.EDU
>>  https://lists.XCF.Berkeley.EDU/mailman/listinfo/advanced-java
>>
>
>
>__________________________________________________
>Do You Yahoo!?
>Tired of spam?  Yahoo! Mail has the best spam protection around
>http://mail.yahoo.com
>_______________________________________________
>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

_______________________________________________
Advanced-java mailing list
Advanced-java at lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/advanced-java

-------------- next part --------------
An HTML attachment was scrubbed...
URL: /lists/advanced-java/attachments/20070430/76d15018/attachment.html 


More information about the Advanced-java mailing list