Java error: Implicit super constructor is undefined for default constructor

来源:互联网 发布:对流风和穿堂风知乎 编辑:程序博客网 时间:2024/04/28 04:54

今天帮忙给同学做一道Java作业题的时候,碰见了这个错误

Implicit super constructor Person() is undefined for default constructor. Must define an explicit constructor

之前没注意这块,记录一下,下面是stackoverflow上的回答:

You get this error because a class which has no constructor has a default constructor, which is argument-less and is equivalent to the following code:

public ACSubClass() {    super();}

However since your BaseClass declares a constructor (and therefore doesn't have the default, no-arg constructor that the compiler would otherwise provide) this is illegal - a class that extends BaseClass can't call super(); because there is not a no-argument constructor in BaseClass.

This is probably a little counter-intuitive because you might think that a subclass automatically has any constructor that the base class has.

The simplest way around this is for the base class to not declare a constructor (and thus have the default, no-arg constructor) or have a declared no-arg constructor (either by itself or alongside any other constructors). But often this approach can't be applied - because you need whatever arguments are being passed into the constructor to construct a legit instance of the class.


0 0
原创粉丝点击