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

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 a toString() method (along with other methods like equals() and hashCode()).

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 custom toString().

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/[deleted] Feb 07 '20 edited Jan 08 '25

abounding stocking marble towering pet attraction lock snow insurance obtainable

This post was mass deleted and anonymized with Redact

-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.