wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Downcasting and overridden methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Downcasting and overridden methods" Watch "Downcasting and overridden methods" New topic
Author

Downcasting and overridden methods

James Baud
Ranch Hand

Joined: Jan 06, 2001
Posts: 60

Can anyone explain why this prints 1 if f() is private in PolyB, else if public it prints 2?
------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
[This message has been edited by James Baud (edited April 10, 2001).]


<B>~James Baud</B><P>He who asks, is a fool for five minutes;<BR>but, he who does not ask, remains a fool forever. (Chinese proverb)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
PolyC can not override a private method in PolyB therefore f() is not eleigible for polymorphism.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Rebecca Pickett
Greenhorn

Joined: Aug 16, 2000
Posts: 15
Originally posted by James Baud:
Can anyone explain why this prints 1 if f() is private in PolyB, else if public it prints 2?
[/B]

James, This is my take on it -- and I could be wrong. But it's a good basic OOP question.
ref1 is a reference to a PolyC object. ref2 is also a reference to the same PolyC object, but it's being cast to a PolyB. If PolyB.f() is private, that means that PolyC.f() does NOT override PolyB.f() with it's own code since private methods are only accessible inside that class. Thus, when PolyB.f() is private and you call PolyB.f() (however indirectly), it's going to print PolyB.f()'s code. When PolyB.f() is public, it is overridden by the PolyC.f() method when the PolyC object is created. I altered your code to print strings out, which helped me make more sense of it. Take a look:

Everyone, if I'm wrong, let me know!

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Here's the explanation:
private methods are also treated as final. Remember, final methods can not be overridden by a subclass so the compiler will always do compile time binding to the method. At compile time you are asking to run the f() method of PolyB. So since the f() method is private (and therefore final) it binds the execution to that method at compile time. At run time it does not matter what object ref2 is actually pointing to... the binding has already been done.
[This message has been edited by Thomas Paul (edited April 11, 2001).]
 
jQuery in Action, 2nd edition
 
subject: Downcasting and overridden methods
 
Similar Threads
Prints 1 why not 2
access modifier
Overriding & private modifier
Queries
Inheritance doubt