I have a some simple Java code that looks similar to this in its structure:
abstract public class BaseClass {
String someString;
public BaseClass(Str
Eclipse will give this error if you don't have call to super class constructor as a first statement in subclass constructor.
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.
You could also get this error when JRE is not set. If so, try adding JRE System Library to your project.
Under Eclipse IDE:
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"); .. }
}
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.
I have resolved above problem as follows: