r/javahelp • • 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();
    
   
    
    }
  1. 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

6 comments sorted by

View all comments

-1

u/desrtfx Out of Coffee error - System halted Feb 07 '20

Have you investigated about .toString()?

Check out Object.toString() and method overriding- then you understand F.

1

u/Saivy__4 Feb 07 '20
package gursimar_navdeep_a2;

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();
       
   @Override 
public String toString(){
return "First Name: " +firstName +" Last Name: " +lastName +"Employee Id:" +id;

}

}

I added tostring(), but still im not able to extend my class. and how do I override "Object"?

1

u/dastardly740 Feb 08 '20

All classes in Javs extend Object implicitly. So, you have overridden Object.toString. Now reread (f) in the directions and do what it says.