When do you need to explicitly call a superclass constructor?

后端 未结 3 1689
忘掉有多难
忘掉有多难 2020-11-27 12:46

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\

3条回答
  •  余生分开走
    2020-11-27 13:22

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

提交回复
热议问题