Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need to call `super` from a constructor?

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...

  1. I need to access this from within the constructor
  2. The superclass constructor requires different arguments then the derived class constructor

Are there other times when I should include a call to the superclass constructor?

like image 206
sfletche Avatar asked Dec 27 '16 19:12

sfletche


People also ask

Does super need to be called in a 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.

Do you have to call super in subclass constructor?

As mentioned above, you only have to invoke a super constructor if there isn't a default constructor in the parent class.

What is the rule for a super reference in a constructor?

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.

Why must super () or this () Be the first statement in a constructor?

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() .

How to call a super class from a constructor?

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.

How to call two constructors at the same time?

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.

What happens if we call'Super ()'without a superclass in Java?

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……

Is it possible to call Super() with arguments?

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).


2 Answers

Yes, that sounds correct, albeit a bit oddly formulated. The rules should be

  • In a derived class, you always1 need to call the super(…) constructor
  • If you are not doing more than the default constructor, you can omit the whole constructor(){}, 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 returning an object, which you hardly ever would.

like image 155
Bergi Avatar answered Oct 18 '22 03:10

Bergi


You need to call super in a subclass constructor in these cases:

  • You want to reference this in the subclass constructor
  • You don't return a different object in the subclass constructor

In 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.

like image 28
Oriol Avatar answered Oct 18 '22 03:10

Oriol