Java error: Implicit super constructor is undefined for default constructor

前端 未结 10 985
借酒劲吻你
借酒劲吻你 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 10:58

    Eclipse will give this error if you don't have call to super class constructor as a first statement in subclass constructor.

    0 讨论(0)
  • 2020-11-22 11:02

    For those who Google for this error and arrive here: there might be another reason for receiving it. Eclipse gives this error when you have project setup - system configuration mismatch.

    For example, if you import Java 1.7 project to Eclipse and you do not have 1.7 correctly set up then you will get this error. Then you can either go to Project - Preference - Java - Compiler and switch to 1.6 or earlier; or go to Window - Preferences - Java - Installed JREs and add/fix your JRE 1.7 installation.

    0 讨论(0)
  • 2020-11-22 11:02

    You could also get this error when JRE is not set. If so, try adding JRE System Library to your project.

    Under Eclipse IDE:

    1. open menu Project --> Properties, or right-click on your project in Package Explorer and choose Properties (Alt+Enter on Windows, Command+I on Mac)
    2. click on Java Build Path then Libraries tab
    3. choose Modulepath or Classpath and press Add Library... button
    4. select JRE System Library then click Next
    5. keep Workspace default JRE selected (you can also take another option) and click Finish
    6. finally press Apply and Close.
    0 讨论(0)
  • Another way is call super() with the required argument as a first statement in derived class constructor.

    public class Sup {
        public Sup(String s) { ...}
    }
    
    public class Sub extends Sup {
        public Sub() { super("hello"); .. }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 11:17

    I have resolved above problem as follows:

    1. Click on Project.
    2. click on properties > Java Build Path > Library > JRE System Library > Edit
    3. Select default system JRE And Finish
    4. Apply and close.
    0 讨论(0)
提交回复
热议问题