Java inheritance - constructors

前端 未结 5 698
盖世英雄少女心
盖世英雄少女心 2021-01-02 04:22

While studying for my finals, I came across the following statement in the book from which I am currently studying. Considering the following code :

class A         


        
5条回答
  •  遥遥无期
    2021-01-02 04:32

    The compiler automatically inserts super() in the beginning.

    However, even constructors arguments, super() (without arguments) is added which invokes the default constructor of the superclass. And you don't have one, hence the error.

    You have to specify super(x) (to invoke A(x)), or define a no-argument constructor.

    By the way, Eclipse compiler gives a way better error message:

    Implicit super constructor A() is undefined. Must explicitly invoke another constructor

提交回复
热议问题