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