Sub topic 10
Subclass construction and superclass constructors
A class describes the data and actions of an object. A subclass is a class that extends another class. The class it extends is called its superclass. This relationship is called inheritance: the subclass can use accessible members, such as methods, defined by the superclass.
For example, an Employee can be a kind of Person. When you create an Employee, Java also runs a Person constructor. A constructor is the part of a class that sets up a new object.
Build a Person and an Employee
Save this program in a file named Main.java:
class Person {
private final String name;
Person(String name) {
this.name = name;
}
String getName() {
return name;
}
}
class Employee extends Person {
private final int employeeId;
Employee(String name, int employeeId) {
super(name);
this.employeeId = employeeId;
}
int getEmployeeId() {
return employeeId;
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Employee("Mina", 42);
System.out.println(employee.getName());
System.out.println(employee.getEmployeeId());
}
}Compile and run it from the folder containing Main.java:
javac Main.java
java MainThe output is:
Mina
42The field named name holds the person's name. It is private, so Employee cannot access it directly. Instead, Employee can use the accessible getName method. The final keyword means each field can be assigned only once.
Follow the constructor calls
The words “extends Person” make Employee a subclass of Person. When the program creates an Employee, it passes two arguments, or values supplied to a constructor: the name and the employee ID.
The Employee constructor then calls super(name). The word super refers to the superclass. This call passes the name to the Person constructor. After that constructor finishes, the Employee constructor sets employeeId.
This process creates one Employee object, not a separate Person object and Employee object. The Employee object has the Person data as well as its own employee ID.
The call to getName works because Employee inherits that accessible method from Person. The method reads Person's private name field. The call to getEmployeeId uses the method defined in Employee.
Choose a superclass constructor
A subclass does not inherit its superclass's constructors. It must call an accessible superclass constructor as part of its own construction. In the example, Person has a constructor that takes a name, so Employee calls super(name).
In this chapter's examples, put the super call first in the subclass constructor. This form works across Java versions.
If a constructor has no explicit call to super or another constructor in the same class, Java inserts a call to super() for you. That call takes no arguments. It works only if the superclass has an accessible constructor that takes no arguments.
To fix that error, call the existing Person constructor with a name, as the example does. Another option would be to add an accessible no-argument constructor to Person and decide what name it should set.
Exercise
Why can an Employee call getName(), but not directly read the name field?
Show answer
Employee inherits the accessible getName method from Person. The name field is private to Person, so Employee cannot read it directly.