Reputation: 19
I am learning about super()
constructor and I ran into this statement:
Since the inherited instance variables should be initialized, and the base class constructor is designed to do that, then an explicit call to
super()
should always be used.
What does this statement mean?
Except for the fact that implicit call can result in an error if the base class has not defined a no-argument constructor?
Upvotes: 0
Views: 84
Reputation: 178
I think the quoted paragraph is a little bit misleading because you only need to explicitly call a parametrised super() if you are not happy with the unparametrised implicit super() call.
You would also have to do a parametrised super() call if the base class only has parametrised constructors.
Upvotes: 2
Reputation: 661
This sentence you quoted doesn't explicitly say that you need to have 0 argument constructor in your class. The point of the sentence is that all the variables that are part of parent class should be initialized and you initialize those variables with a constructor, therefore you should call parent class constructor inside of your child class constructor. Doesn't matter what is the number of arguments of parent class constructor.
Upvotes: 1