• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mock test question about Parent and Child constructors.  RSS feed

 
Ranch Hand
Posts: 32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm taking some mock test on skill-guru.com and I found this question:


What is the Output of the Program ? Select the one Correct answer

The answer is "Child Show 0 Child Show 20" but I don't quite understand why x is printing cero and why is it calling the show method from Chil while in the constructor's parent.

I appreciate if you can explain a little bit about why this is happening.


Regards,

 
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable x in the class Child hides the variable x in the class Base. (Base.x is still there, we just can't see it unless we use the super keyword.)

You know that there is an implicit call to the superclass constructor. But realize that when this superclass call is made the varlable Child.x has not yet been initialized. Until we return from the superclass constructor, its value will remain the default value of zero. Only then will the initialization in the subclass occur.

The subclass version of show is called because even though the superclass constructor is called, it is a Child instance that has been created, and it is that implementation that can be executed.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The base class constructor is called from the implicitly placed super() in the child's constructor. I hope you understand this.

When the super class constructor, the object in creation is the child class object. So in the super class constructor, when you try to call an overridden method, the method present in the actual object will be called.

Try marking the super class method as either final or private and note the difference.

We had a recent discussion on this topic. I will get that thread for you.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the example given by Rob http://www.coderanch.com/t/561068/java/java/Constructor#2547318
 
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought a constructor is not able to access instance methods unless its super constructor has completed. This does not appear to be the case here.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The super constructor is first run and it's the code in the super constructor that calls the child's instance method. Code in child's constructor runs only after the call to super() finishes. Your thought was correct Ziggy.
 
Gabriela Rios
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! It all makes sense now.
 
What are your superhero powers? Go ahead and try them on this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!