I have a class ProductionWorker with the superclass Employee, that I created for a school project. We are supposed to set up exceptions for it this week and I keep getting an error when trying to create one of them. Here's my code:
in ProductionWorker:
public String toString() throws InvalidShift{
DecimalFormat dollar = new DecimalFormat("$#,##0.00");
String str = super.toString();
str += "The employee's pay rate is " + dollar.format(getPayRate()) +"\n";
str += "The employee works the " + getShift() + " shift";
return str;
}
The super.toString class:
public String toString() throws InvalidShift{
String str = "The employee's name is " + getEmployeeName() + ".\n";
if (employeeNumber.equals("")){
str += "The employee's ID number is invalid.\n";
}else{
str += "The Employee's ID number is " + employeeNumber + ".\n";
}
str += "The employee's hire date was " + hireDate + ".\n";
return str;
}
When I try to compile, I get the following errors:
Employee.java:126: error: toString() in Employee cannot override toString() in Object
public String toString() throws InvalidShift{
^
overridden method does not throw InvalidShift
ProductionWorker.java:54: error: toString() in ProductionWorker cannot override toString() in Object
public String toString() throws InvalidShift{
^
overridden method does not throw InvalidShift
Now, the actual exception itself is thrown in the getShift() method, so there's no way that the Employee.toString() method could actually throw the exception. The program would enter the ProductionWorker.toString(), run the Employee.toString() and return it's string, then execute getShift() later, which could throw the exception. So I don't need Employee.toString() to be capable of throwing, and it would cause a mess of other problems if it did.
Any help?
Edit
I'm going to just rewrite the whole thing to be an int instead of two booleans.