Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does a call from a child to a method in parent class,have to go through the parent constructor

 
Ashish Jha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java..
please help me understand the steps that follow once i call the method of the parent class.. why does the call have to go through the constructor first in some cases?
in constructor of class B, I tried different ways and the variation of output, I am not able to understand..

here's the code:




 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. If you don't call the superclass constructor in a subclass constructor, Java adds it for you automatically. The reason is that a child class can't be created unless the parent class exists.

I'm not clear on exactly what you are trying to do. Why do you need to call a method without the constructor being called?
 
Ashish Jha
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, i was just trying to see the program flow in different situations.. how control flows when making a call in different ways..
thanks for reply Jeanne
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic