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 . ?
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.
Joined: Nov 24, 2011
Cause then i was immediately asked if suppose you don't have any initialization to be done .. then .?
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.
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.
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.
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)
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