简体   繁体   中英

Java - Cannot Access Abstract Method from Subclass Object

I'm working on a Java project where I have a superclass called Personnel , and two subclasses; Manager and Worker .

The subclasses contain separate variables that are used to hold information about their pay, and I previously had method getSalary() for Manager and getWage() for Worker . However, I wanted a number of these to be accessible through a for-loop so I put an abstract method into the Personnel class called getAnnualIncome() and put a corresponding method into both subclasses:

public abstract class Personnel
{
//attributes of each instance of class
protected String name;
protected int payrollNum;
protected String dept;

//abstract method to be defined in subclasses
public abstract float getAnnualIncome();
}

In Manager :

public float getAnnualIncome()
{
    //convert salary variable to real number
    float salaryAsFloat = salary;

    //return real number
    return salaryAsFloat;
}

In Worker :

public float getAnnualIncome()
{
    //number of weeks in a year as constant value
    final int weeksInYear = 52;

    //weekly wage by number of weeks in a year
    return ((hourlyRate * hoursPerWeek) * weeksInYear);
}

This all works fine in theory, but when I came to actually implementing the for-loop I mentioned (using an array of the Personnel class with objects from both subclasses as elements), the following piece of code did not recognize the getAnnualIncome() method (this is taken from the executable main class):

for (int index = 0; index < employee.length; ++index)
{
    System.out.printf(employee[index].getName()
    + "\t\t" + "£%.2f %n", employee[index].getAnnualIncome());
                          //error message appears here ^^ 'method is undefined in 'Personnel'
}

Not really sure what's going wrong here, thought I'd covered everything that needed to be done. As you can see from my code extracts, the method is in the superclass!

I don't really know where to go from here, any shove in the right direction would be greatly appreciated!

Thanks, Mark

Are you able to create an array of the Personnel class with objects from both subclasses as elements? because that itself will give you error. "Can not convert subclass(Worker/ Manager) to Personnel[].

In case that works fine for you, than the problem is with the annotation @Override.

When you made an abstract class method, you are telling the JVM, "Childs" from this "Parent" WILL have those methods, and will either perform them, or have their "Childs" perform them.

Absctract class methods cannot use Objects details in themselfs, they are not able to be instantiated.

Either have getAnnualIncome() in both Worker and Manager (and their "Children") or have that abstract class do the method.

To fix your issue, use the annotation @Override, to inform Java 6+ that the code is legitimated (while optional, this could help), and check that the hierarchy is correct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM