Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes What is the output of the program ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the output of the program ?" Watch "What is the output of the program ?" New topic
Author

What is the output of the program ?

vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
Hello.... Friends!!

Please tell me what is the out put of following program

I think the output should be Base Show 10 Child Show 20

but it is giving the output Child Show 0 Child Show 20

Thanks in advance!!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Why not run it yourself and see?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
I have already executed the code it is giving the output Child Show 0 Child Show 20 ,

Still not getting why it is Child Show 0 Child Show 20 and why not Base Show 10 Child Show 20.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

vishal mishra wrote:I have already executed the code it is giving the output Child Show 0 Child Show 20 ,

Still not getting why it is Child Show 0 Child Show 20 and why not Base Show 10 Child Show 20.


Basically, polymorphism applies, even before the object has fully initialized. The overridden method is always called. As for why it is zero instead of 20, that is caused by the variable not being initialized yet.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Henry Wong wrote:
vishal mishra wrote:I have already executed the code it is giving the output Child Show 0 Child Show 20 ,

Still not getting why it is Child Show 0 Child Show 20 and why not Base Show 10 Child Show 20.


Basically, polymorphism applies, even before the object has fully initialized. The overridden method is always called. As for why it is zero instead of 20, that is caused by the variable not being initialized yet.

Henry

Sir, is it because in this code show() is accessing the instance x variable before the execution of Child class' constructor completed ??


OCPJP 6 86%
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Nikhil Sagar wrote: . . . Sir, is it because in this code show() is accessing the instance x variable before the execution of Child class' constructor completed ??
Child is not a good name for a subclass; Derived would have been better.

It is accessing the show() method not only before the subclass’ constructor completes, but before the superclass’ constructor completes. The details are in the Java Language Specification, maybe this section.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

Campbell Ritchie wrote:
Nikhil Sagar wrote: . . . Sir, is it because in this code show() is accessing the instance x variable before the execution of Child class' constructor completed ??
Child is not a good name for a subclass; Derived would have been better.

It is accessing the show() method not only before the subclass’ constructor completes, but before the superclass’ constructor completes. The details are in the Java Language Specification, maybe this section.


Thanks for links sherrif,
actually i was addressing to the Child class of this particular code.

vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
Thanks to all of you !! Thank you !!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
You’re welcome
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

i remember asking the question during days of my OCPJP preparation. i remember Mr. Henry sir answered with the exact same words that overriding happens even BEFORE the object is initialized. i just wonder it must require hell lot of patience to answer same question 100 times with same effort and zeal as if answering for the very first time. maybe that is what takes to be a moderator or i must say the position wherever the guys like henry, ritchie, winston, mathew, stephan and all the moderators are
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the output of the program ?