1. Overview
1.概述
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.
在本教程中,我们将仔细研究Java“隐式超级构造函数未定义”错误。首先,我们将创建一个如何产生它的例子。接下来,我们将解释导致该异常的主要原因,随后我们将看到如何修复它。
2. Practical Example
2.实例
Now, let’s see an example that generates a compilation error “Implicit super constructor X() is undefined. Must explicitly invoke another constructor”.
现在,让我们看一个例子,它产生了一个编译错误 “隐式超级构造函数X()未定义。必须明确调用另一个构造函数”。
Here, X represents the parent class that is extended by any subclass that sees this error.
这里,X代表父类,它被任何看到这个错误的子类所扩展。
First, let’s create a parent class Person:
首先,让我们创建一个父类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:
现在,让我们创建一个子类Employee,它的父类是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:
现在,在我们的IDE中,我们会看到这个错误。
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:
例如,让我们考虑没有构造函数的Employee。
public class Employee extends Person {
Double salary;
// setters and getters
}
We’ll see the error in our IDE:
我们将在我们的IDE中看到这个错误。
Implicit super constructor Person() is undefined for default constructor. Must define an explicit constructor
3. Cause
3.原因
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.
在 Java 继承中,构造函数链指的是使用super方法调用一连串的构造函数,以便从父类中链出构造函数。子类构造函数必须调用超类构造函数,无论是显式还是隐式。无论哪种方式,都必须定义一个超级构造函数。。
A class that has no parent has the Object class as its parent. The Object class in Java has a constructor with no arguments.
一个没有父类的类将Object类作为其父类。Java中的Object类有一个没有参数的构造函数。
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.
当一个类没有构造函数时,编译器会添加一个不需要参数的默认构造函数,在第一条语句中,编译器插入对super的调用–它调用Object类的构造函数。
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:
让我们假设我们的Person类不包含任何构造函数,也没有父类。一旦我们进行编译,我们可以看到编译器已经添加了默认的构造函数。
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.
相反,如果在Person类中已经有一个构造函数,编译器就不会添加这个默认的、没有args的构造函数。
Now, if we create subclass Employee that extends Person, we get an error in the Employee class:
现在,如果我们创建扩展Person的子类Employee,我们在Employee类中得到一个错误。
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.
由于编译器将插入对Employee构造函数的super调用,它不会在父类Person.中找到一个不带参数的构造函数。
4. Solution
4.解决办法
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:
我们需要做的第一件事是,从Employee构造函数中显式调用super构造函数。
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:
现在,假设我们需要创建一个只有salary字段的Employee对象。让我们来编写构造函数。
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:
尽管在Employee构造函数中添加了super调用,我们仍然收到一个错误,因为Person类仍然缺少一个匹配的构造函数。我们可以通过在Person类中明确创建一个无参数的构造函数来解决这个问题。
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
5.总结
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.
我们已经解释了Java的 “隐式超级构造函数未定义 “错误。然后,我们讨论了如何产生该错误和异常的原因。最后,我们讨论了解决该错误的方案。
As always, the example code for this article is available over on GitHub.
像往常一样,本文的示例代码可在GitHub上获得。