Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!