When do you need to explicitly call a superclass constructor?
constructor, java, subclass, superclass
Solution
You never need just
super();
That's what will be there if you don't specify anything else. You only need to specify the constructor to call if:
- You want to call a superclass constructor which has parameters
- You want to chain to another constructor in the same class instead of the superclass constructor
You claim that:
At the same time I've also seen instances on here where someone's problem was not explicitly calling super().
Could you give any examples? I can't imagine how that's possible...
Problem
So say I have a subclass that extends a superclass. In what scenarios do I need to explicitly type `super()` to get the superclass constructor to run? I'm looking at an example in a book about abstract classes and when they extend it with a non-abstract subclass, the subclass's default constructor is blank and there's a comment that says the superclass's default constructor will be called. At the same time I've also seen instances on here where someone's problem was not explicitly calling `super()`. Is the distinction from calling the superclass's default/non-default constructor from the subclass's default/non-default constructor?