• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

What is the output of the program ?

 
Ranch Hand
Posts: 103
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not run it yourself and see?
 
vishal mishra
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 216
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Marshal
Posts: 76825
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all of you !! Thank you !!
 
Campbell Ritchie
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic