Why we can't use this() and super() both in a constructor? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why we can't use this() and super() both in a constructor?

6th Jun 2019, 3:41 PM
Avinash Maurya
Avinash Maurya - avatar
2 Answers
+ 1
Can you give an example or a code?
7th Jun 2019, 12:54 AM
Denise Roßberg
Denise Roßberg - avatar
0
It's just a rule that deals with the requirements and make it clear enough. The first instruction a constructor executes, is calling another constructor, once and only once. That other constructor can be a constructor of the same class, with this(), or of the superclass, with super(). If the programmer doesn't call any constructor at the beginning of a constructor, then the instruction super(); is implicitly added by the compiler. Everything is done so that calling another constructor is done once and only once, at the beginning. (It needs to be at the beginning, because the superclass needs to finish its initialization before the current class starts computing its own.)
7th Jun 2019, 8:03 AM
kumesana