Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling super() on a class with no constructor

I just noticed in the codebase of a very large project I am assigned at work, a particular class has no constructor. Its subclass though, calls super().

Can someone please explain what is going when a subclass calls super(), but there is no constructor in the parent?

(I can guess that the effect is like calling an empty constructor, but I am wondering if there is anything else going on behind the scenes).

like image 783
Josh Avatar asked Dec 19 '22 20:12

Josh


1 Answers

If you do not have any parameterized constructor and I strictly mean no constructor then and only then java will add a default constructor(One with no parameters) for you.

Each constructor must call a constructor of it's super class. You cannot perform any other action in the subclass constructor untill all constructors of superclasses up the inheritance tree are called. Hence this call must be the 1st line of your subclass constructor If you do not provide one again java does that for you.

Refer

Why does this() and super() have to be the first statement in a constructor?

like image 142
Aniket Thakur Avatar answered Jan 12 '23 17:01

Aniket Thakur