• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the output of the program ?

 
vishal mishra
Ranch Hand
Posts: 103
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not run it yourself and see?
 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21227
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nikhil Sagar
Ranch Hand
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 216
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you !! Thank you !!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic