Why "super" keyword must be the first statement in constructor | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why "super" keyword must be the first statement in constructor

13th Apr 2018, 12:57 PM
Nashat
Nashat - avatar
3 Answers
+ 11
Because a child cannot exist without parents... Just like in real life. A child object cannot do anything before the parent object is initialized. If you don't write it down, it will get called implicitly to make sure the parent class exists.
13th Apr 2018, 1:20 PM
Tashi N
Tashi N - avatar
+ 3
The superclass' [parent/ base] constructor needs to be called before the subclass' constructor. This will ensure that if you call any methods on the superclass in your constructor, the superclass has already been set up correctly. In other words, superclass acts as the tree and the subclass acts as its fruit; which means without the source you can’t obtain the object. As @Tashi N mentioned: parent-> superclass, baby-> subclass. Good luck! CA
13th Apr 2018, 2:41 PM
Cris Ahmad
Cris Ahmad - avatar
0
Cris Ahmad thank you 😊
13th Apr 2018, 2:58 PM
Nashat
Nashat - avatar