In this article, we will look at “Implicit super constructor is undefined” error message in java in eclipse or any other IDE, its reason and solution with examples.
Below are the steps to produce “Implicit super constructor is undefined error”.
- Create two classes with parent-child relation between them, such as
Connection
(Parent) andTCPConnection
(Child). - Define a parameterized constructor in both these classes. A parameterized constructor is one which takes at least one argument.
- You will see “Implicit super constructor is undefined error Connection() is undefined” over
TCPConnection
or child class constructor.
Program example written as per above scenario is given below
class Connection { public Connection(int p) { } } class TCPConnection extends Connection { public TCPConnection(int c) { // error } }
If you are writing this in an IDE, the error will be at the child constructor as shown above.
If you are writing in a text editor and compiling using command prompt or terminal, then you will get below error
error: constructor Connection in class Connection cannot be applied to given types;
Reason
To understand the reason or cause of this error, it is necessary to know below two important pointers regarding java constructors.
- When constructor of a class is invoked, it implicitly invokes the constructor of its parent or super class.
If you do not call super class constructor yourself, the compiler calls no-argument or default constructor of parent class. - Compiler automatically creates a default or no-argument constructor in a class. This is created only if no other constructor is present.
If you explicitly define a constructor, default constructor will not be created.
Keeping these two things under consideration, when we define constructor of TCPConnection
(Child) class, under the hood the compiler translates it to
public TCPConnection(int c) { super(); }
And since, we have defined a parameterized constructor in Connection
(parent) class, the compiler did not generate a default constructor.
So, super()
does not find a matching constructor and hence, we get error “Implicit super constructor is undefined error Connection() is undefined”
There are two ways to resolve this error.
-
- Define an explicit default constructor in parent class.
As stated before, if a class contains a constructor, the compiler does not automatically generate default constructor.
So, define a default constructor yourself. ModifiedConnection
(parent) class will beclass Connection { public Connection(int p) { } // default constructor public Connection() { } }
- Explicitly invoke parent class constructor from within the child constructor using
super()
.
Remember, invoking parent class constructor should be the first statement in child class constructor.
Modified child constructor will beclass TCPConnection extends Connection { public TCPConnection(int s) { super(s); } }
- Define an explicit default constructor in parent class.
Error in eclipse
Eclipse or any other IDE might sometimes flag “Implicit super constructor is undefined error Connection() is undefined” error inappropriately.
That is, parent child relation and their constructors might not be an issue.
This error might also arise due to incorrect java version configured in your project.
To resolve it, follow any of the below steps.
- Go to Window -> Preferences -> Java -> Compiler
and select relevant java compiler version. - Go to Window -> Preferences -> Java -> Installed JREs
and select or add the relevant JRE. - Right Click the project. Select Build Path -> Configure Build Path
and select relevant JRE under libraries tab.