Forums Register Login

Calling constructor from another constructor

+Pie Number of slices to send: Send
Hi,

I would like to know, why we cannot call the same class contructor from another constructor of the same class along with calling a super class constructor. what may be the rationale behind it.

For ex.

+Pie Number of slices to send: Send
 

nitinram agarwal wrote:Hi,
I would like to know, why we cannot call the same class contructor from another constructor of the same class along with calling a super class constructor. what may be the rationale behind it.
[/code]



The reason is because the compiler won't allow you to call the constructor of the super class twice. Hence, it will either allow you to call the super() constructor (as the first line), or the this() constructor (as the first line). If you use the this() constructor, the super() constructor will actually not be called (no implicit call), because it will be assumed that the other constructor will do it.

In your case, you can't call both the super() and this() because this will attempt to construct the super class twice. The other constructor doesn't have a call to either super() or this(), and hence, has an implicit call to the super() no-arg constructor.

Henry
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2437 times.
Similar Threads
A simple Q but still have doubt
Base Class , Super class constructors ****
keyword Super
Super class constructor
Constructor
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 22:51:42.