r/javahelp • u/Saivy__4 • Feb 07 '20
Need Help with Abstract Classes!
abstract class Employee {
private int id;
private String firstName;
private String lastName;
public Employee(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
abstract double calculatePay();
}
Employee class – This class has the following characteristics a. Must be abstract b. Three data fields i. id: int (greater than 0)
ii. firstName: String
iii. lastName: String
c. Only one constructor, namely a 3-arg constructor setting the above
d. Getters and Setters for the above
e. An abstract calculatePay method with no parameters that returns a double
f. Overrides the toString() method of Object making it abstract
Im not able to understand (F) part or last part. Any hints will be greatly appreciated.
Thank You
17
Upvotes
19
u/Northeastpaw Feb 07 '20
Every object that doesn't explicitly extend another object implicitly extends java.lang.Object which defines
toString(). This means every class, including your Employee class, has atoString()method (along with other methods likeequals()andhashCode()).Normally you override
toString()if you need to customize it. The default implementation in java.lang.Object usually isn't very helpful. A class isn't required to override it, but by overriding it in Employee and making it abstract you are requiring that any class extending Employee must implement a customtoString().