educative.io

Educative

Is super() mandatory for a subclass?

Very Important: The call to the SuperClass constructor using super() should always be the first line of code inside the constructor of the SubClass.

super() is not mandatory for a subclass. The statement above and the included code snippet for that chapter is confusing. If a subclass is initialising only its own fields using its constructor, then there should not be any need of super() call. Isn’t it?

Hi @Rishabh_Raj ,

This is Fatimah Abdullah from Educative. Thank you for reaching out to us!

In response to your question, you are right it is not always needed. If the subclass is only initializing its own fields then even if it does not specify super() in the first line, the default no-args constructor of the parent class will be automatically called. I have updated the lesson to remove this confusion. Thank you!

Best Regards,

Fatimah Abdullah | Developer Advocate
Educative

1 Like