Super key word | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Super key word

if we use super in sub class constructor then the sub class constructor is executing frst but if we doesn't use super and use just inheritance concept then the super class is executing first.... can u explain y??

19th Jan 2017, 10:15 AM
Sathwik Gaddi
Sathwik Gaddi - avatar
3 Answers
+ 1
The constructor of the superclass is ALWAYS executed first. Sometimes explicitly via super() in the first line of the subclasses constructor, because you want to pass an argument to the superclass constructor. And sometimes its calls the superclass constructor "silently". But it is ALWAYS called regardless
19th Jan 2017, 11:26 PM
Jannick
0
actually I called the constructor in the subclass then according to theory the constructor which is called is to be executed right??
20th Jan 2017, 1:58 AM
Sathwik Gaddi
Sathwik Gaddi - avatar
0
Yes calling the constructor of the superclass in the constructor of the subclass is very common, if the superclass constructor takes arguments. Anyway, if you call the constructor of the superclass in the constructor of the subclass, it has to be the first thing you do in this constructor, which also makes it clear, that the constructor of the superclass is always called first
20th Jan 2017, 5:37 AM
Jannick