At this point, you may feel like quibbling. What would it mean to “inherit” your parents' birth experiences? Well, you can stop right there. This is not an effort to form a perfect metaphor. It only helps to introduce an important fact about Java programming — the fact that classes don't inherit constructors from their parent classes.
Check out this code: What Is an Employee?
package com.allyourcode.company;
public class Employee {
public String name;
public String jobTitle;
public Employee() {
}
public Employee(String name, String jobTitle) {
this.name = name;
this.jobTitle = jobTitle;
}
<strong> public String getPayString() {</strong>
<strong> return name + ", Pay not known\n";</strong>
<strong> }</strong>
}
Now examine this code: Full-Time Employees Have Salaries
package com.allyourcode.company;
import java.text.NumberFormat;
import java.util.Locale;
public class FullTimeEmployee extends Employee {
<strong> public double salary;</strong>
static NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);
public FullTimeEmployee() {
}
public FullTimeEmployee(String name, String jobTitle, double salary) {
this.name = name;
this.jobTitle = jobTitle;
this.salary = salary;
}
<strong> public double pay() {</strong>
<strong> return salary;</strong>
<strong> }</strong>
<strong> </strong>
<strong> @Override</strong>
<strong> public String getPayString() {</strong>
<strong> return name + ", " + currency.format(pay()) + "\n";</strong>
<strong> }</strong>
}
Look at the constructors.
The FullTimeEmployee
class extends the Employee
class.
Both classes have parameterless constructors.
Both classes have constructors that initialize all of their fields.
In fact, a FullTimeEmployee
constructor initializes three fields. Only one of these fields — the salary
field — is declared in the FullTimeEmployee
class's code. The FullTimeEmployee
class inherits the other two fields — name
and jobTitle
— from the Employee
class. This isn't a matter of FullTimeEmployee
overriding its parent class's constructors. There are no constructors to override. Like any other subclass, the FullTimeEmployee
class doesn't inherit its parent class's constructors.
Is there any way to avoid the loathsome redundancy of all the constructor declarations? There is. Java's super
keyword can refer to a parent class's constructor:
public FullTimeEmployee(String name, String jobTitle, double salary) {
super(name, jobTitle);
this.salary = salary;
}
In this code, the FullTimeEmployee
constructor calls its parent class's constructor. The call to super
has two parameters and, as luck would have it, the parent Employee
class has a two-parameter constructor:
public Employee(String name, String jobTitle) {
this.name = name;
this.jobTitle = jobTitle;
}
The super
call sends two parameters to the parent class's constructor, and the parent class's constructor uses those two parameters to give name
and jobTitle
their initial values. Finally, the FullTimeEmployee
class assigns a value to its own salary
field. Everything works very nicely.