Correct way to build constructors in a subclass that extends to a subclass of a superclass

时间:2018-03-22 23:25:47

标签: java constructor subclass superclass

edit: added employee constructors;

my question involves constructor chaining for a subclass (to a super which is itself a subclass). I have written constructors that seem to work, but I feel that they are incorrectly written as I will explain. PTEmployee extends to Employee who is extended to a Person. The code below seems to work in my test class,

public class PartTimeEmployee extends Employee {

public static final int DEFAULT_HOURSPRWK = 0;
private int hoursPerWk;

public PartTimeEmployee() {
    this(DEFAULT_HOURSPRWK);
}

public PartTimeEmployee(int hoursPerWk) {
    this(DEFAULT_HIRE_DATE, DEFAULT_INCOME, hoursPerWk);
}

public PartTimeEmployee(String hireDate, double incomeWk, int hoursPerWk) {
    super(hireDate, incomeWk);
    this.hoursPerWk = hoursPerWk; // I dont think I need two this.var?
}

public PartTimeEmployee(String firstName, String surname, int age, Address address, String hireDate, double incomeWk, int hoursPerWk) {
    super(firstName, surname, age, address, hireDate, incomeWk);
    this.hoursPerWk = hoursPerWk;
}

But I feel that the use of two constructors with (super) and this.hoursPerWk = hoursPerWk is wrong, shouldn't '(super)' and 'this.var = var' only need to be written once? If I adjust the code to remove the PTEmp(hiredate, incomewk, hrswk) constructor than I get a 'no constructor found' error in my second constructor. Other edits have led to recursor errors.

So the supplied code works and calls all details in my test class, but is it correctly written (I need to have a Person class that is extended by Employee which is extended by PTEmp or Boss etc.). Appreciate any feedback (or jsut to know this is correct if it is.

Thanks.

Added Employee constructors here...

public class Employee extends Person {

public static final String DEFAULT_HIRE_DATE = "00/00/00";
public static final double DEFAULT_INCOME = 0;

private String hireDate;
private double incomeWk;

public Employee() {
    this(DEFAULT_HIRE_DATE, DEFAULT_INCOME);
}

public Employee(String hireDate, double incomeWk) {
    this(DEFAULT_FIRSTNAME, DEFAULT_SURNAME, DEFAULT_AGE, new Address(), hireDate, incomeWk);
}

public Employee(String firstName, String surname, int age, Address address, String hireDate, double incomeWk) {
    super(firstName, surname, age, address);
    this.hireDate = hireDate;
    this.incomeWk = incomeWk;
}

1 个答案:

答案 0 :(得分:0)

Not sure what super() does if you don't specify names, address, etc, but assuming that you can use some defaults, maybe you can call it like this:

public PartTimeEmployee(String hireDate, double incomeWk, int hoursPerWk) {
    this(DEFAULT_FIRST_NAME, DEFAULT_SURNAME, DEFAULT_AGE, DEFAULT_ADDRESS, hireDate, incomeWk, hoursPerWk);
}

Consider using builder patterns, if you think that you have too many overloaded constructors:

When would you use the Builder Pattern?