方法不会覆盖或实现超类型的方法

时间:2012-09-17 20:38:20

标签: java inheritance override abstract

我正在使用继承和多态创建一个模拟员工数据库。尝试覆盖超类方法时,我遇到了以下错误。

HourlyEmployee is not abstract and does not override abstract method resetWeek() in Employee
public class HourlyEmployee extends Employee
   ^
HourlyEmployee.java:43: error: method does not override or implement a method from a supertype
@Override
^
HourlyEmployee.java:54: error: method does not override or implement a method from a supertype
@Override
^
HourlyEmployee.java:60: error: method does not override or implement a method from a supertype
@Override
^
HourlyEmployee.java:66: error: method does not override or implement a method from a supertype
@Override

这是我的Employee Superclass和HourlyEmployee Subclass代码

public abstract class Employee
{
protected String firstName;
protected String lastName;
protected char middleInitial;
protected boolean fulltime;
private char gender;
private int employeeNum;

public Employee (String fn, String ln, char m, char g, int empNum, boolean ft)
{
    firstName = fn;
    lastName = ln;
    middleInitial = m;
    gender = g;
    employeeNum = empNum;
    fulltime = ft;
}

public int getEmployeeNumber()
{
    return employeeNum;
}

public void setEmployeeNumber(int empNum)
{
    while (empNum <= 10000 && empNum >= 99999)
    {
        System.out.print ("Invalid input, please try again: ");
    }

    if (empNum >= 10000 && empNum <= 99999)
    {
        employeeNum = empNum;
    }
}

public String getFirstName()
{
    return firstName;
}

public String getLastName()
{
    return lastName;
}

public char checkGender(char g)
{
    if (g != 'M' || g != 'F')
    {
        g = 'F';
    }
    return g;
}

public char getGender()
{
    return gender;
}


@Override
public boolean equals(Object e2)
{
    if (this.employeeNum == ((Employee)e2).employeeNum)
    {
        return true;
    }
    else
    {
        return false;
    }
}

@Override
public String toString()
{
    return employeeNum + "\n" + lastName + ", " + firstName + "\n" + "Gender:" + gender + "\n" + "Status:" + fulltime + "\n";
}

public abstract double caclulateWeeklyPay();

public abstract void annualRaise();

public abstract double holidayBonus();

public abstract void resetWeek();
}

这是HourlyEmployee子类

public class HourlyEmployee extends Employee
{
private double wage;
private double hoursWorked;
private double additionalHours;

public HourlyEmployee(String fn, String ln, char m, char g, int empNum, boolean ft, double w, double h, double ahours)
{
    super (fn, ln, m, g, empNum, ft);
    wage = w;
    hoursWorked = h;
    hoursWorked = 0.0;
    additionalHours = ahours;
}

@Override
public String toString()
{
    return this.getEmployeeNumber() + "\n" + lastName + ", " + firstName + middleInitial + "\n" + "Gender: "
     + this.getGender() + "\n" + "Status: " + fulltime + "\n" + "Wage: " + wage + "\n" + "Hours Worked: " + hoursWorked + "\n";
}

   //@Override    
public double calculateWeeklyPay(double w, double h)
{
    if (h > 40)
    {
        w = w * 2;
    }

    return w * h;        
}

//@Override
public double annualRaise(double w)
{
    return (w * .05) + w;
}

//@Override
public double holidayBonus(double w)
{
    return w * 40;
}

//@Override
public double resetWeek(double h)
{
    h = 0.0;
    return h;
}

public double plusHoursWorked(double h, double ahours)
{
    while (h <= 0)
    {
        System.out.println("Invalid value entered, please try again");
    }

    h += ahours;

    return h;
}


}

3 个答案:

答案 0 :(得分:12)

您的Employee类有4个抽象方法,其中没有一个是实现的(至少没有正确实现)。这是一个例子:

double caclulateWeeklyPay(); // in Employee
double calculateWeeklyPay(double w, double h) // in HourlyEmployee

父类应包含相同的签名(包括参数),并且应如下所示:

public abstract double calculateWeeklyPay(double w, double h);

由于这似乎是作业,我会把剩下的留给你。

答案 1 :(得分:3)

请仔细阅读错误消息:

  

HourlyEmployee不是抽象的,并且不会覆盖Employee中的抽象方法resetWeek()

它清楚地表明您的Employee班级是abstract并且有一个抽象方法resetWeek()

HourlyEmployee扩展的类Employee不是抽象的,因此它应该覆盖超类的所有抽象方法,因为具体的类不能有任何抽象方法。

原因是HourlyEmployee类中的方法与超类中的方法具有不同的签名。因此,不会覆盖该方法,并且编译器不接受@Override注释

答案 2 :(得分:1)

由于您的 HourlyEmployee * 类不是抽象的 *您需要实现您的EMPLOYEE抽象类中声明的抽象方法。这很明显。 你必须实现以下方法

 public abstract double caclulateWeeklyPay();

 public abstract void annualRaise();

 public abstract double holidayBonus();

 public abstract void resetWeek();