aspose file tools*
The moose likes Java in General and the fly likes Constructor Chaining Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Constructor Chaining" Watch "Constructor Chaining" New topic
Author

Constructor Chaining

Mukul Anand
Greenhorn

Joined: Nov 28, 2012
Posts: 29
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

Joined: Aug 16, 2005
Posts: 14432
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mukul Anand
Greenhorn

Joined: Nov 28, 2012
Posts: 29
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

Joined: May 30, 2012
Posts: 679
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
The Java Language Specification describes the process, and also mandates the process. It may be difficult to understand, however.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Class or superclass or interface are just description of object. Object is always created with new keyword !


True person is moral, false is right!
Mukul Anand
Greenhorn

Joined: Nov 28, 2012
Posts: 29
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.
 
jQuery in Action, 2nd edition
 
subject: Constructor Chaining