1. Overview

In this tutorial, we’ll take a closer look at the Java “implicit super constructor is undefined” error. First, we’ll create an example of how to produce it. Next, we’ll explain the leading cause of the exception, and later we’ll see how to fix it.

2. Practical Example

Now, let’s see an example that generates a compilation error “Implicit super constructor X() is undefined. Must explicitly invoke another constructor”.

Here, X represents the parent class that is extended by any subclass that sees this error.

First, let’s create a parent class Person:

public class Person {

    String name;
    Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

   // setters and getters
}

Now, let’s create a subclass Employee whose parent is Person:

public class Employee extends Person {

    Double salary;

    public Employee(String name, Integer age, Double salary) {
        this.salary = salary;
    }

    // setters and getters
}

Now, in our IDE, we’ll see the error:

Implicit super constructor Person() is undefined. Must explicitly invoke another constructor

In some cases, we can get a similar error if the child class doesn’t have a constructor.

For instance, let’s consider the Employee with no constructor:

public class Employee extends Person {

    Double salary;

    // setters and getters
}

We’ll see the error in our IDE:

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

3. Cause

In Java inheritance, constructor chaining refers to calling a sequence of constructors using the super method to chain constructors from the parent class. Subclass constructors must invoke the superclass constructor, either explicitly or implicitly. Either way, a super constructor must be defined.

A class that has no parent has the Object class as its parent. The Object class in Java has a constructor with no arguments.

When a class does not have a constructor, the compiler adds a default constructor that takes no arguments, and in the first statement, the compiler inserts a call to super – which calls the constructor of the Object class.

Let’s assume that our Person class doesn’t contain any constructor and has no parent. Once we compile, we can see the compiler has added the default constructor:

public Person() {
    super();
}

In contrast, if there is already a constructor in the Person class, this default, no-args constructor is not added by the compiler.

Now, if we create subclass Employee that extends Person, we get an error in the Employee class:

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

Since the compiler will insert a super call to the Employee constructor, it won’t find a constructor without parameters in the parent class Person.

4. Solution

To resolve this error, we need to explicitly provide information to the compiler.

The first thing we need to do is to explicitly call the super constructor from the Employee constructor:

public Employee(String name, Integer age, Double salary) {
    super(name, age);
    this.salary = salary;
}

Now, let’s say we need to create an Employee object with only the salary field. Let’s write the constructor:

public Employee(Double salary) {
    super();
    this.salary = salary;
}

Despite adding the super call to the Employee constructor, we still receive an error because the Person class still lacks a matching constructor. We can fix this by creating an argument-less constructor explicitly in the Person class:

public Person(String name, Integer age) {
    this.name = name;
    this.age = age;
}

public Person() {
}

Finally, thanks to these changes, we won’t get compilation errors.

5. Conclusion

We’ve explained Java’s “implicit super constructor is undefined” error. Then, we discussed how to produce the error and the cause of the exception. Lastly, we discussed a solution to resolve the error.

As always, the example code for this article is available over on GitHub.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.