Hey guys! So the error I'm getting is "cannot reference hoursWorked before supertype constructor has been called". I've just recently learned about using abstract, extends, and the keyword "super". What should I do in order to get rid of the error? Am I missing something? Here's my code so far:

Employee.java

public abstract class Employee {
    private String name;

    public Employee(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double computeTax(){
        double amount = 0;

        return amount;
    }

}

HourlyEmployee.java

public abstract class HourlyEmployee extends Employee{
    protected double hoursWorked;

    public HourlyEmployee(String name, double hoursWorked) {
        super(name);
        this.hoursWorked = hoursWorked;
    }

    public double getHoursWorked() {
        return hoursWorked;
    }

    public void setHoursWorked(double hoursWorked) {
        this.hoursWorked = hoursWorked;
    }

Janitor.java

public class Janitor extends HourlyEmployee{
    private double hourlyRate;

    public Janitor(String name, int hourlyRate) {
        super(name, hoursWorked);
        this.hourlyRate = hourlyRate;
    }

}

Lawyer.java

public class Lawyer extends HourlyEmployee {
    private double consultationFee;

    public Lawyer(String name, double consultationFee) {
        super(name, hoursWorked);
        this.consultationFee = consultationFee;
    }

}

Thanks for taking time to help me out!

Edited 2 Years Ago by pritaeas: Moved.

hoursWorked is defined and initialised in HourlyEmployee.
In class Janitor's constructor you have
super(name, hoursWorked);
now the parameters of a method (or constructor) are evaluated before the method is called, so this line tries to evaluate hoursWorked before it can call HourlyEmployee's constructor. Because HourlyEmployee's contructor has not yet been called, hoursWorked has not yet been fully instantiated, so it's not possible to use it.

This is a fault in your design. In Janitor's constructor what do you think the value of hoursWorked is/should be? You certainly have not provided one.

Well, the value of hoursWorked varies between each object of the Janitor and Lawyer class. How should I change the design of my program?

Sounds like you need to supply a value for hoursWorked in the parameters for the constructors for Janitor and Lawyer, then you can pass those values up to the superclass constructor
and/or
pass a default value (eg 0) to the superclass constructor, then have a setHoursWorked method to update it.

Here's what I have under my main class:

public class PayrollMainPhase1 {

    public static void main(String[] args) {
        List<Employee> employees = new ArrayList<Employee>();

        HourlyEmployee louis = new Janitor ("Louis Litt", 70); 
        HourlyEmployee harvey = new Lawyer ("Harvey Specter", 750);
        HourlyEmployee mike = new Lawyer ("Mike Ross", 400);

        louis.setHoursWorked(217.5);
        harvey.setHoursWorked(100);
        mike.setHoursWorked(150);

        employees.add(new Accountant ("Rachel Zane", 19500));
        employees.add(new Accountant ("Donna Paulsen", 8000));
        employees.add(louis);
        employees.add(harvey);
        employees.add(mike);

        for(Employee e : employees){
            System.out.println(e.toString());
            System.out.println("==================");
        }
    }

}

OK, that fits the second solution in my previous post. Initialise hoursWorked to zero by passing zero to the superclass constructor

ps System.out.println(e.toString()); the toString is superfluous in println because prinltln always calls toString for all its parameters anyway, so you can just say
System.out.println(e);

Hi laguardian, how did you initialise hoursWorked to zero by passing zero to the superclass constructor?

eg:

public Janitor(String name, int hourlyRate) {
   super(name, 0);  // passes the name, and zero for the hours, to the superclass contructor
   this.hourlyRate = hourlyRate;
}

Edited 2 Years Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.