using super() in java, hourly employee method

300 views Asked by At

I'm having problems with one of my classes. Everything is fine except for the line with the super(hourly) under public HourlyEmployee(HourlyEmployee hourly). I don't know if I'm using super wrong or how to fix it. It just says "actual and formal arguments differ in length. Not sure what that means." thanks

package payrollsystem_1;
import java.util.ArrayList;

public class HourlyEmployee extends Employee {
    private double hourlyRate;
    private double periodHours;

    public HourlyEmployee(int employeeID, String firstName, String lastName, 
            ArrayList<Paycheck> listOfPaychecks, double hourlyRate, double periodHours ){

        super(employeeID, firstName, lastName, listOfPaychecks);
        this.listOfPaychecks = listOfPaychecks;
        this.hourlyRate = hourlyRate;
        this.periodHours = periodHours;
    }

    public HourlyEmployee(HourlyEmployee hourly) {
       super(hourly);
        this.hourlyRate = hourly.hourlyRate;
        this.periodHours = hourly.periodHours;
    }

    public double getHourlyRate(){
        return hourlyRate;
    }

    public void setHourlyRate(double hourlyRate) {
        this.hourlyRate = hourlyRate;
    }

    public double getPeriodHours() {
        return periodHours;
    }

    public void setPeriodHours(double periodHours) {
        this.periodHours = periodHours;
    }

}
1

There are 1 answers

2
EI CHO On

You need to make sure that do there has any constructors like

public Employee(HourlyEmployee hourly) {
  //I know the super class shouldn't know about the subclass.
  //But this is OK if you write like this.
  //It can be compiled without showing any errors.
  /*code*/
}

or

public Employee(Employee hourly) {
  /*code*/
}

in your Employee class.If the super class 'Employee' do not have constructor like those two mentioned above. You will get the message "actual and formal arguments differ in length" when you try to compile HourlyEmployee.java.

This means that your super class 'Employee' do not have a constructor that need to pass a HourlyEmployee or it's super class instance to.

In fact, You need to show more informations about the compiler error message.I guess you got sth like this.

HourlyEmployee.java:xx: error: constructor Employee in class Entity cannot be applied to the given types:
public Employee(int employeeID, String firstName, String lastName, ArrayList<Object> listOfPaychecks)
    required: int,String,String,ArrayList<Paycheck>
    found: HourlyEmployee
    reason: actual and formal argument lists differ in length