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

relation between child and parent class

 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If child class is instantiated then the constructor of parents class is called first(super() is added by jvm in constructor of child class), so my question is while creating object of child class whether the object of parent class is created or not
for example


Output:
Parent Constructor
Child Constructor

Because super() is added by jvm as the first statement in constructor of Child class so I wanted to know whether the object of Parent class is created or not. how?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one object being created, the instance of Child. But the Child IS-A Parent since it extends Parent, so the single Object that is being created must be a fully functional (and initialized) Parent as well as a fully functional (and initialized) Child. So the first thing that happens is the single Object is created as a Parent first, then 'extended' to include all the initializations for the Child.
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:There is only one object being created, the instance of Child. But the Child IS-A Parent since it extends Parent, so the single Object that is being created must be a fully functional (and initialized) Parent as well as a fully functional (and initialized) Child. So the first thing that happens is the single Object is created as a Parent first, then 'extended' to include all the initializations for the Child.


Thank You very much Steve
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic