+ 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??
3 Antworten
+ 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
0
actually I called the constructor in the subclass then according to theory the constructor which is called is to be executed right??
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