I have an abstract super class of Employee I have 3 subclasses: 1. Salary 2. Hou
ID: 3627361 • Letter: I
Question
I have an abstract super class of EmployeeI have 3 subclasses:
1. Salary
2. Hourly
3. Commission
I use an array of employees to create each. Once I have created some of these there is a submenu that asks if the user wants to add hours to a specific employee based on employee number. So I create a method in the hourly class that is unique to hourly. Right now I am just trying to call it to print something to the screen to see it works. However I can't get the syntax down.
Currently I have:
public void addHours()
{
for (int i=0; i<currentEmployees; i++)
{
if (employees[i] instanceof HourlyEmployee)
{
employees[i].increaseHours();
}
}
}//end addHours
This is the method it is trying to call which is in the hourly subclass:
public void increaseHours()
{
System.out.print("Testing Method Valid");
}
When I compile it I get:
EmployeeManager.java:58: cannot find symbol
symbol : method increaseHours()
location: class Employee
employees[i].increaseHours();
^
1 error
Does anyone know what I am doing wrong with this?
Explanation / Answer
Hi, below is the incorrect usage of the keyword "instanceof."
if (employees[i] instanceof HourlyEmployee)
To fix this, you could place a stub of the "increaseHours()" function in the abstract "Employee" class then override it in the "Hourly" class. Then remove your "addHours()" function and call "increaseHours()" explicitly.
Why this didn't work: the instanceof is usually a last resort and confused your employee array as "employee" objects and not your distinctive ones and applied the function to salary and commisson employees thus you recieved the "cannot find symbol" error
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.