aspose file tools*
The moose likes Beginning Java and the fly likes Why Constructor Chaining ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Constructor Chaining ?" Watch "Why Constructor Chaining ?" New topic
Author

Why Constructor Chaining ?

AnuragSingh Kumar
Greenhorn

Joined: Nov 24, 2011
Posts: 5
Recently I came across this question
"Constructor Chaining means invoking the super class(es) constructor first, let them complete their code and then complete the child class constructor. Any special benefit ??"

All i could answer was as it may help in getting variables of super class initialized before the subclass. However i didn't myself felt that this may be the correct explanation.
Can i please get some help to figure this out . ?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

AnuragSingh Kumar wrote:All i could answer was as it may help in getting variables of super class initialized before the subclass. However i didn't myself felt that this may be the correct explanation.

why?
AnuragSingh Kumar
Greenhorn

Joined: Nov 24, 2011
Posts: 5
Cause then i was immediately asked if suppose you don't have any initialization to be done .. then .?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18160
    
    8

But you do have initialization to do. Maybe your class doesn't, but its superclass does. (You extended Object, remember?) So if your class's constructor doesn't get called then neither does Object's constructor.
Avinash Ga
Ranch Hand

Joined: Aug 13, 2011
Posts: 78

Constructor chaining does not mean only the invocation of the super class constructor. We can always chain the constructor with other constructor of the same class to achieve pre-defined defaults.

like.


but if the number of properties increases in a class then constructor chaining approach will complicate the things and thats the time when you dump constructor chaining and go for much cleaned Builder Pattern.

Best Wishes.


Avinash G.A
OCP Java SE 6 Programmer, OCP Java EE 5 Web Component Developer, OCE Java EE 6 Web Services Developer, VMware Certified Core Spring 3.x Developer, EMC Proven Professional (ISM-V2)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Avinash Ga wrote:



it should be this(false, true) not this.ConstructorClass(false, true); and fine about telescoping constructor pattern which uses constructor chain of same class. even the last called constructor must call super class constructor because you cant do recursive constructor invocation. i.e, you cant do this



Avinash Ga
Ranch Hand

Joined: Aug 13, 2011
Posts: 78

OOps,,, sorry my mistake......
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Cool
 
Don't get me started about those stupid light bulbs.
 
subject: Why Constructor Chaining ?
 
Similar Threads
Reg. this and super
Super class constructor
Question from Dan's test
Constructor Question
Clarifications/Suggestions in Java