Reading Dr. Axel Rauschmayer's blog on ES6 classes, I understand that a derived class has the following default constructor when none is provided
constructor(...args) {
super(...args);
}
I also understand that if I want to use this
within a constructor I first need to call super
, otherwise this
will not yet be initialized (throwing a ReferenceError).
constructor(width, height) {
this.width = width; // ReferenceError
super(width, height);
this.height = height; // no error thrown
...
}
Is the following assumption then correct? (and if not, could you please explain the conditions under which I should explicitly call super
)
For derived classes, I only need to explicitly call super
when...
this
from within the constructorAre there other times when I should include a call to the superclass constructor?
When your superclass doesn't have a no-arg constructor, the compiler will require you to call super with the appropriate arguments. The compiler will make sure that you instantiate the class correctly.
As mentioned above, you only have to invoke a super constructor if there isn't a default constructor in the parent class.
Call to super() must be first statement in Derived(Student) Class constructor. If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.
The Eclipse compiler says, Constructor call must be the first statement in a constructor . So, it is not stopping you from executing logic before the call to super() . It is just stopping you from executing logic that you can't fit into a single expression. There are similar rules for calling this() .
If the SuperClass has a default Constructor there is no need to call it using "super ()" explicitly, it will be invoked implicitly. Actually, nothing will be displayed. Since the class named Object is the superclass of all classes in Java.
At the time of object creation, only one constructor can be called. Through super, we can call the other constructor from within the current constructor when needed. If you are thinking why it's there for a class that is not extending any other class, then just remember every class follows object class by default.
Since the class named Object is the superclass of all classes in Java. If you call "super ()" without any superclass, Internally, the default constructor of the Object class will be invoked (which displays nothing). //Nothing will be displayed……
However, you may use the call to super () with arguments if the parent's constructor takes parameters, and you wish to specify them. Moreover, if the parent's constructor takes parameters, and it has no default parameter-less constructor, you will need to call super () with argument (s).
Yes, that sounds correct, albeit a bit oddly formulated. The rules should be
super(…)
constructorconstructor(){}
,
which in turn will make your class code not contain a super call.1: You don't need to call it in the suspicious edge case of explicitly return
ing an object, which you hardly ever would.
You need to call super
in a subclass constructor in these cases:
this
in the subclass constructorIn other cases, you can call it if you want the superclass constructor to run, but you don't have to.
class SuperClass{
constructor() {
console.log('SuperClass');
}
}
class SubClass1 extends SuperClass {
constructor() {
console.log('SubClass1');
super();
return {};
}
}
class SubClass2 extends SuperClass {
constructor() {
console.log('SubClass2');
return {};
}
}
new SubClass1();
new SubClass2();
I don't see how the order of arguments matters when deciding whether you should call super
or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With