When do you need to explicitly call a superclass constructor?

60,134

Solution 1

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

Solution 2

If you don't explicitly call a super constructor the argument less constructor (super()) will be called. This means you have to call a specific constructor yourself if there's no reachable argument-less constructor of the super class.

But often enough you want a different constructor anyways even if you could use the default constructor - depends on your code.

Also note that if no constructor is declared the compiler generates a public default constructor automatically, but as soon as you write your own constructor this does not happen anymore.

Solution 3

The super() method is always called in constructors of sub-classes, even if it is not explicitly written in code.

The only time you need to write it, is if there are several super(...) methods in the super-class with different initialization parameters.

Share:
60,134
jhlu87
Author by

jhlu87

Trader learning to program

Updated on February 08, 2020

Comments

  • jhlu87
    jhlu87 about 4 years

    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?

  • Voo
    Voo almost 13 years
    "The only time you need to write it, is if there are several super(...) methods in the superclass with different initialization parameters." Not true. As long as there's a reachable argument less constructor, you can have a hundred other constructors without any problems.
  • jhlu87
    jhlu87 almost 13 years
    Yea I should've been more clear. It was super(with arguments). Thanks for your answer.
  • jhlu87
    jhlu87 almost 13 years
    Just to make sure I understand. If in my superclass, I have one constructor and it's not the default constructor(say it takes a parameter). Then in my constructor for my subclass I need to explicitly call super(param) because the compiler will no longer generate a default constructor for the super class?
  • ACV
    ACV about 8 years
    You are right. But about this: 'At the same time I've also seen instances on here where someone's problem was not explicitly calling super().' This happens when parent constructor does not have a noarg constructor. In that case if you don't explicitly call super() you will get an error.
  • Jon Skeet
    Jon Skeet about 8 years
    @ACV: Explicitly calling super() won't work there either. Calling a superconstructor with a non-empty argument list would work, but just using super() wouldn't.
  • patrik
    patrik over 6 years
    @JonSkeet Sometimes see a lot of super()s in the code I work with. I do almost clinically remove these whenever I see them. However, I cannot help wonder if there is a reason why they are there. Would you be able to think of a case where you actually want these no args constructors?
  • Jon Skeet
    Jon Skeet over 6 years
    @patrik: Well there's a difference between requiring the constructor to be present, and requiring an explicit delegation to the parameterless super-constructor. The latter is a matter of style - whether you prefer to be explicit/obvious or brief.