Java error: Implicit super constructor is undefined for default constructor

前端 未结 10 1098
借酒劲吻你
借酒劲吻你 2020-11-22 10:28

I have a some simple Java code that looks similar to this in its structure:

abstract public class BaseClass {
    String someString;
    public BaseClass(Str         


        
10条回答
  •  悲&欢浪女
    2020-11-22 11:07

    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.

提交回复
热议问题