This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes A small doubt while calling overriden method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A small doubt while calling overriden method" Watch "A small doubt while calling overriden method" New topic
Author

A small doubt while calling overriden method

Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87



o/p :-

Inside Foo Constructor
Inside Bar Show
Inside Bar Constructor


My doubt is why is it calling method of Bar class , when i do this.show() rather show() of Foo Class in Foo constructor the Bar object is still not completely created. But it seems polymorphism is not depended up on object creation.


Please explain the above behaviour , Thanks in advance
[ March 03, 2008: Message edited by: Neha Mohit ]
jim xu
Greenhorn

Joined: Nov 18, 2007
Posts: 6
Since the object created is Bar's object, so "this" should reference to the Bar's object. And because the method is overridden, Bar object has no visibility of Foo's show. Try change the signature of the method for foo and call it from Bar's object, you will see that it actually calls Foo's show again, in that case these two shows become just two different methods (happened to have the same name) Interesting enough is that changing the access level can also change the calling sequence.

[ March 04, 2008: Message edited by: jim xu ]
[ March 04, 2008: Message edited by: jim xu ]
 
Consider Paul's rocket mass heater.
 
subject: A small doubt while calling overriden method