Java error: implicit super constructor undefined for default constructor

I have simple Java code that is similar to it in its structure:

abstract public class BaseClass { String someString; public BaseClass(String someString) { this.someString = someString; } abstract public String getName(); } public class ACSubClass extends BaseClass { public ASubClass(String someString) { super(someString); } public String getName() { return "name value for ASubClass"; } } 

I will have quite a few subclasses of BaseClass , each of which implements the getName() method in its own way ( template template template ).

This works well, but I don't like having a redundant constructor in subclasses. It is more to type and it is difficult to maintain. If I had to change the signature of the BaseClass constructor BaseClass , I would have to change all subclasses.

When I remove the constructor from subclasses, I get this compile-time error:

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

Am I trying to do this?

+83
java inheritance dry boilerplate
Jul 29 '09 at 1:28
source share
9 answers

You get this error because a class that does not have a constructor has a default constructor that has no arguments and is equivalent to the following code:

 public ACSubClass() { super(); } 

However, since your BaseClass declares a constructor (and therefore does not have a default no-arg constructor that the compiler otherwise provided), this is illegal - a class that extends BaseClass cannot call super(); , because there is no constructor with no arguments in BaseClass.

This is probably a little contrary to intuition, because you might think that a subclass automatically has a constructor that has a base class.

The easiest way is that the base class does not declare a constructor (and therefore has a default constructor, no-arg) or has a declared no-arg constructor (by itself or next to any other constructors). But often this approach cannot be applied - because you need all the arguments that are passed to the constructor to create a legitimate instance of the class.

+135
Jul 29 '09 at 1:36
source share

For those who google for this error and arrive here: there may be another reason for getting it. Eclipse gives this error if you have a project setup - a system configuration mismatch.

For example, if you import a Java 1.7 project into Eclipse and you do not have the correct 1.7 configuration, you will get this error. Then you can go to Project - Preference - Java - Compiler and switch to 1.6 or earlier ; or go to Window - Preferences - Java - Installed JREs and add / fix the installation of JRE 1.7.

+39
Feb 19 '13 at 20:30
source share

Perhaps, but not the way you have it.

You need to add the no-args constructor to the base class and what it is!

 public abstract class A { private String name; public A(){ this.name = getName(); } public abstract String getName(); public String toString(){ return "simple class name: " + this.getClass().getSimpleName() + " name:\"" + this.name + "\""; } } class B extends A { public String getName(){ return "my name is B"; } public static void main( String [] args ) { System.out.println( new C() ); } } class C extends A { public String getName() { return "Zee"; } } 

If you do not add the constructor (any) to the class, the compiler adds the default constructor no arg for you.

When defualt no arg calls super (); and since you don’t have it in the superclass, you will get this error message.

As for the question, this is itself.

Now, expanding the answer:

Do you know that creating a subclass (behavior) to indicate a different value (data) does not make sense? !!! I hope you do it.

If the only change is the "name", then just one parameterized class is enough!

Therefore, you do not need this:

 MyClass a = new A("A"); MyClass b = new B("B"); MyClass c = new C("C"); MyClass d = new D("D"); 

or

 MyClass a = new A(); // internally setting "A" "B", "C" etc. MyClass b = new B(); MyClass c = new C(); MyClass d = new D(); 

When you can write this:

 MyClass a = new MyClass("A"); MyClass b = new MyClass("B"); MyClass c = new MyClass("C"); MyClass d = new MyClass("D"); 

If I had to change the signature of the BaseClass constructor method, I would have to change all subclasses.

It’s good that inheritance is an artifact that creates HIGH cohesion, which is undesirable in OO systems. It should be avoided and possibly replaced with a composition.

Think about whether they are really needed as a subclass. This is why you often come across interfaces that were used by insted:

  public interface NameAware { public String getName(); } class A implements NameAware ... class B implements NameAware ... class C ... etc. 

Here, B and C could inherit from A, which would create a very high connection between them using interfaces, the connection will decrease, if A decides that there will no longer be "NameAware", the rest of the classes will not be violated.

Of course, if you want to reuse the behavior, this will not work.

+7
Jul 29 '09 at 1:35
source share

your BaseClass needs someString to deliver it.
Try the following:

 abstract public class BaseClass { String someString; public BaseClass(String someString) { this.someString = someString; } abstract public String getName(); } public class ACSubClass extends BaseClass { public ASubClass() { super("someString"); // or a meaningfull value, same as getName()? } public String getName() { return "name value for ASubClass"; } } 

or

 abstract public class BaseClass { String someString; protected BaseClass() { // for subclasses this.someString = null; // or a meaningfull value } public BaseClass(String someString) { this.someString = someString; } abstract public String getName(); } public class ACSubClass extends BaseClass { public String getName() { return "name value for ASubClass"; } } 
0
Jul 29 '09 at 11:47
source share

Another way is to call super () with the required argument as the first statement of the constructor of the derived class.

 public class Sup { public Sup(String s) { ...} } public class Sub extends Sup { public Sub() { super("hello"); .. } } 
0
Jun 08 '14 at 22:51
source share

Eclipse will provide this error if you do not have the superclass constructor called as the first statement in the subclass constructor.

0
Apr 02 '15 at 15:41
source share

This error occurred due to incorrect JRE settings, so follow these steps:




1. right-click the project -> Properties -> Java Build Path -> Libraries Tab
2. Click Add Library
3. Select the JRE System Library β†’ Click "Next" 4. Select "Workspace" by default jre or alternative installed jre
5. Click Finish
0
Aug 31 '16 at 0:21
source share

You can solve this error by adding a constructor with no arguments to the base class (as shown below).

Greetings.

  abstract public class BaseClass { // ADD AN ARGUMENTLESS CONSTRUCTOR TO THE BASE CLASS public BaseClass(){ } String someString; public BaseClass(String someString) { this.someString = someString; } abstract public String getName(); } public class ACSubClass extends BaseClass { public ASubClass(String someString) { super(someString); } public String getName() { return "name value for ASubClass"; } } 
-one
Aug 20 '13 at 21:17
source share

I had this error and fixed it by removing the exception from the exception from the method in the try / catch block

For example: FROM:

 public static HashMap<String, String> getMap() throws SQLException { } 

TO:

 public static Hashmap<String,String> getMap() { try{ }catch(SQLException) { } } 
-one
Jul 19 '16 at 18:38
source share



All Articles