Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor Chaining

 
Mukul Anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object is created for a subclass.
The constructors are called all the way up to the Object class. But do the objects of all the super classes (of course the non-abstract ones) get created in the process?
If not, then what exactly happens when those super class constructors are called?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can see an object as an onion, that has different layers.

Suppose you have class Super, and class Sub extends Super. When you create an instance of class Sub, then you get an object which consists of three layers: an Object, then on top of that whatever is in class Super, and on top of that whatever is in class Sub.

Note that inheritance means that there's an "is a" relationship from the subclass to the superclass. This corresponds to what I described above. A Sub is a Super, which is an Object.

When you create a new object, first the core is created (the Object part), then the layers on top of that are created.
 
Mukul Anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So how does the JVM differentiate between these constructor calls?
I mean, how does it know that those super constructor calls are part of constructor chaining and hence separate objects of those super classes need not be created?
Because the call(s) to the super constructor in that "chain reaction" are no different from a normal "new" invocation of those super constructors. Isn't it?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukul Anand wrote:Ok. So how does the JVM differentiate between these constructor calls?

It doesn't need to. Constructors, despite their name, do not create objects. Only one object is created by a new call and the object already exists before any constructor is called. The constructors merely sets the object to its initial state.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Language Specification describes the process, and also mandates the process. It may be difficult to understand, however.
 
Volodymyr Lysenko
Ranch Hand
Posts: 512
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class or superclass or interface are just description of object. Object is always created with new keyword !
 
Mukul Anand
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
Mukul Anand wrote:Ok. So how does the JVM differentiate between these constructor calls?

It doesn't need to. Constructors, despite their name, do not create objects. Only one object is created by a new call and the object already exists before any constructor is called. The constructors merely sets the object to its initial state.


Hmm. That clarifies things !!
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic