• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Constructor Chaining ?  RSS feed

 
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 . ?
 
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 .?
 
Sheriff
Posts: 22846
43
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.
 
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
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's 9 years laters but thats the charm of a forum, ;-)

I understand the technical process of constructor chaining but what is the purpose in real life coding? why do you use it?

You can also just call the 3rd constructor by calling the constructor with 2 parameters directly.

I just like to understand the reason why programmers use it in real life.
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean 5 years later
 
Saloon Keeper
Posts: 7994
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This matter is the same for constructor chaining and method delegation: If the method or constructor doesn't do anything useful other than simplifying an interface, I don't call it from inside the class.

If there's only one constructor that does everything, and the rest just call it with default values, then they all call the one constructor. The same for convenience methods. However, as soon as one does even a little bit of extra work, I think it's preferable to delegate to that constructor or method instead, because it gives you less places where you can mess up if you have to change the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!