Thursday, 17 July 2014

A Common Error Message: Implicit super constructor is undefined for default constructor

This is a compilation error message seen by a lot of Java developers:
"Implicit super constructor is undefined for default constructor. Must define an explicit constructor"
Implicit super constructor is undefined for default constructor
This compilation error occurs because the default super constructor is undefined. In Java, if a class does not define a constructor, compiler will insert a default no-argument constructor for the class by default. If a constructor is defined in Super class, in this case Super(String s), compiler will not insert the default no-argument constructor. This is the situation for the Super class above.
The constructors of the Sub class, either with-argument or no-argument, will call the no-argument Super constructor. Since compiler tries to insert super() to the 2 constructors in the Sub class, but the Super's default constructor is not defined, compiler reports the error message.
To fix this problem, simply 1) add a Super() constructor to the Super class like
public Super(){
    System.out.println("Super");
}
, or 2) remove the self-defined Super constructor, or 3) add super(value) to sub constructors.

No comments:

Post a Comment