Reputation: 11206
I'm trying to write a payroll program that simply receives input, does some calculations and displays the output for EACH employee.
I have two types of employees, hourly and salary. I created a class for each type that extends to an abstract employee class:
public abstract class Employee{
private String name;
private String type;
private int hours;
private double rate;
}
public class SalaryEmployee extends Employee{
public SalaryEmployee(String type, String name, int hours, double rate){
this.type = type;
this.name = name;
this.hours = hours;
this.rate = rate;
}
public void print(){
}
}
In my main method, when I try to create my objects using the code below:
Employee employee;
if(type.equals("hourly"))
{
employee = new HourlyEmployee(type, name, hours, rate);
}
else
{
employee = new SalaryEmployee(type, name, hours, rate);
}
I get the following error when I try to compile:
Lab3.java:53: not a statement
SalaryEmployee employee = new SalaryEmployee(type, name, hours, rate);
^
Lab3.java:53: ';' expected
SalaryEmployee employee = new SalaryEmployee(type, name, hours, rate);
^
2 errors
Any idea what is happening here? I've looked around for messing semi colons or braces, but haven't found anything yet.
Thanks for looking.
Upvotes: 0
Views: 1456
Reputation: 26167
The only error I can see is that your SalaryEmployee
class cannot access the base class variables because they are private
. You should make them protected
.
Although, the error you are getting would be different.. If you are still getting the error after changing, your error is probably lying elsewhere.
Upvotes: 2
Reputation: 47954
It is illegal to declare a variable in an if that does not use braces. It looks like what you meant to write is something like this:
Employee employee;
if(type.equals("hourly"))
employee = new HourlyEmployee(type, name, hours, rate);
else
employee = new SalaryEmployee(type, name, hours, rate);
Most coding conventions support always using braces after an if or else.
Upvotes: 2