Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Constructor Chaining ?

 
AnuragSingh Kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cause then i was immediately asked if suppose you don't have any initialization to be done .. then .?
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OOps,,, sorry my mistake......
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic