Example - Factoring Person Out of Employee

Contact Us or call 1-877-932-8228
Example - Factoring Person Out of Employee

Example - Factoring Person Out of Employee

Since personnel and probably other people in our overall corporate software suite (contact management, perhaps) would have some basic personal attributes in common, we will pull the first and last name fields into a base class representing a person.

  1. Create a new class, Person, in the employees package (that may not be the best place for it, but for convenience we will put it there).
  2. Cut the first and last name fields and the associated get and set methods out of Employee and paste them here (including getFullName).
  3. Create a constructor for Person that sets the first and last names; it would probably b e a good idea to create a default constructor as well.
  4. Declare Employee to extend Person; change the constructor that accepts the first and last name fields to call a super-constructor to accomplish that task.
  5. Note that if getPayInfo() uses firstName and lastname directly, you will need to revise it to call the get methods for those values - why?
  6. We can test the code using a simplified version of Payroll.

Code Sample:

package employees;

public class Person {
  private String firstName;
  private String lastName;
  public Person() {

  public Person(String firstName, String lastName) {

  public String getFirstName() { return firstName; }

  public void setFirstName(String firstName) {
    this.firstName = firstName;

  public String getLastName() { return lastName; }

  public void setLastName(String lastName) {
    this.lastName = lastName;

  public String getFullName() {
    return firstName + " " + lastName;


This class includes the name fields and related set and get methods.

Code Sample:

package employees;

public class Employee extends Person {

---- C O D E   O M I T T E D ----
	public Employee() {
	public Employee(String firstName, String lastName) {
		super(firstName, lastName);
	public Employee(String firstName,String lastName, int dept) {
		super(firstName, lastName);
	public Employee(String firstName, String lastName, double payRate) {
		super(firstName, lastName);
	public Employee(
		String firstName, String lastName, int dept, double payRate) {
	this(firstName, lastName, dept);

---- C O D E   O M I T T E D ----
public String getPayInfo() {
return "Employee " + id + " dept " + dept + " " +
			getFullName() + 
			" paid " + payRate;

Since this class now extends Person, the name-related elements are already present, so we remove them from this code. We took advantage of the Person constructor that accepst first and last names in the corresponding Employee constructor.

Note that since getPayInfo calls getFullName, which is now inherited and publicly accessible, that code did not need to change.

Code Sample:

import employees.*;
import util.*;

public class Payroll {  
  public static void main(String[] args) {
    String fName = null;
    String lName = null;
    int dept = 0;
    double payRate = 0.0;
    double hours = 0.0;

    Employee e = null;

    fName = KeyboardReader.getPromptedString("Enter first name: ");
    lName = KeyboardReader.getPromptedString("Enter last name: ");
    dept = KeyboardReader.getPromptedInt("Enter department: ");
    do {
      payRate = KeyboardReader.getPromptedFloat("Enter pay rate: ");
      if (payRate < 0.0) System.out.println("Pay rate must be >= 0");
    } while (payRate < 0.0);
    e = new Employee(fName, lName, dept, payRate);

No changes need to be made to Payroll to take advantage of the addition of the inheritance hierarchy that we added. The only changes we made were for the sake of brevity.

To revisit the sequence of events when instantiating an Employee using the constructor that accepts the first and last names, department, and pay rate:

  1. Memory for an Object is allocated.
  2. Any Object initializers would run.
  3. The Object() constructor would run.
  4. Memory for a Person is allocated.
  5. If there were any Person initializers, they would run.
  6. The Person constructor would run, because that was the version selected by the Employee constructor we called.
  7. Memory for an Employee is allocated.
  8. If there were any Employee initializers, they would run.
  9. Any additional steps in the Employee constructor we called would run.