Java Reference
In-Depth Information
Remember, your
Employee
class should extend this
Person
class. Also, the variables
manager
and
employeeID
are added to the
Employee
class, since not all
Person
instances will have a
manager
,
but all
Employee
instances will.
public class Employee extends Person {
private Employee manager;
private int id;
public Employee(String name, Employee manager, int empID) {
super(name);
this.setManager(manager);
this.setEmployeeID(empID);
}
public Employee getManager() {
return manager;
}
public void setManager(Employee manager) {
this.manager = manager;
}
public int getEmployeeID() {
return id;
}
private void setEmployeeID(int employeeID) {
this.id = employeeID;
}
}
Notice how the
super
keyword is used in the first statement of the constructor. This means that the
Employee
should be constructed as a
Person
is constructed, then specialized with the
manager
and
id
.
Similar to specializing a subclass constructor by first calling the super constructor, you can also spe-
cialize methods by first calling a super method. Suppose you want to add a
displayName()
method
to the
Employee
class that allows you to format their name badge.
public String displayName(){
return "Employee: " + super.getName();
}
In the
displayName()
method, you have a specialized method for
Employee
that calls the method
of the superclass to get some of the necessary information.
You can also access superclass variables using the
super
keyword. Suppose in your
Person
class
you have an
int
variable called
id
that you use to uniquely identify people in your database. But
you still have the
int
variable
id
in the
Employee
class that you use to store their employee ID. You
might use the following method to return a String with both
id
numbers:
public String displayIdentification() {
Search WWH ::
Custom Search