AnthonyOGC
AnthonyOGC

Reputation: 13

Difficulties using parent class constructors in subclass

I have a Contractor subclass that extends of it's parent class Staff.

    Test Class

    Date macHireDate = new Date(2016, 1, 5);
    Staff mac = new Staff("Mac", 66222222, macHireDate);
    System.out.println(mac);

    Staff bob = new Contractor("Bob", new Date(2012,10,10), new Date(2013,4,11))
    System.out.println(bob);

I want the output to be Bob (ID: None) ... But I'm not sure how to make the ID a String. I don't want to change anything in my Test Class

public class Contractor extends Staff {
private Date contractEnds;

This is my Contractor classes constructor

public Contractor(String name, Date hireDate, long employementID, Date contractEnds) {
    super(name, employementID, hireDate);
    this.contractEnds = contractEnds;
}

and it's extending off of

public Staff(String name, long employementID, Date hireDate) {
super();
this.name = name;
this.hireDate = hireDate;
this.employementID = employementID;
}

Bob only has a String, Date, Date. So I'm having difficulties on how to make bobs ID "None" without changing anything in Test Class.

Upvotes: 0

Views: 50

Answers (4)

FaceFTW
FaceFTW

Reputation: 63

Use a combination of a super() constructor, and any other additional parameters that need to be initialized, to instantiate the object.

public Contractor(String name, Date hireDate, Date contractEnds, int employmentID ) {
    super(name, employmentID, hireDate);
    this.contractEnds = contractEnds;
}

The super() constructor inherits all of the superclass's constructor code, and initializes all of the corresponding variables in the subclass

you can also write custom code in your methods by adding the @Override annotation before it Example:

@Override
public String toString(){...}

Upvotes: 1

user6948706
user6948706

Reputation:

The following code:

public class JavaApplication23
{
    public static void main(String[] args)
    {
        Date macHireDate = new Date(2016, 1, 5);
        Staff mac = new Staff("Mac", 66222222, macHireDate);
        System.out.println(mac);

        Staff bob = new Contractor("Bob", new Date(2012,10,10), 1234L, new Date(2013,4,11));
        System.out.println(bob);

        Staff bob2 = new Contractor("Bob2", new Date(2012,10,10), new Date(2013,4,11));
        System.out.println(bob2);
    }

}

class Contractor extends Staff
{
    private Date contractEnds;

    public Contractor(String name, Date hireDate, long employementID, Date contractEnds)
    {
        super(name, employementID, hireDate);
        this.contractEnds = contractEnds;
    }

    public Contractor(String name, Date hireDate, Date contractEnds)
    {
        super(name, hireDate);
        this.contractEnds = contractEnds;
    }

    @Override
    public String toString()
    {
        String out = super.toString();
        return out + "ContractEnds: " + contractEnds + "\n";
    }
}

class Staff
{
    private String name;
    private long employementID;
    private boolean idIsOK;
    private Date hireDate;

    Staff(String name, long employementID, Date hireDate)
    {
        //super();
        this.name = name;
        this.hireDate = hireDate;
        this.employementID = employementID;
        idIsOK = true;
    }

    Staff(String name, Date hireDate)
    {
        //super();
        this.name = name;
        this.hireDate = hireDate;
        this.employementID = -1;
        idIsOK = false;
    }

    @Override
    public String toString()
    {
        String out = "Name: " + name + "\n";
        out += "HireDate: " + hireDate + "\n";
        out += "EmployementID: " + ((idIsOK) ? employementID : "None") + "\n";
        return out;
    }
}

returns:

Name: Mac
HireDate: Sat Feb 05 00:00:00 CET 3916
EmployementID: 66222222

Name: Bob
HireDate: Sun Nov 10 00:00:00 CET 3912
EmployementID: 1234
ContractEnds: Sun May 11 00:00:00 CEST 3913

Name: Bob2
HireDate: Sun Nov 10 00:00:00 CET 3912
EmployementID: None
ContractEnds: Sun May 11 00:00:00 CEST 3913

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44834

If you change the constructor of Contractor to be

public Contractor(String name, Date hireDate, Date contractEnds) {
    super(name, 0, hireDate);
    this.contractEnds = contractEnds;
}

then also change your toString method of Staff so that if employementID is 0 then it will print None

Upvotes: 1

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

Add a constructor to Contractor that does not require the ID. Use "this" to call the main constructor with a default ID:

[Warning - untested code ahead]

public Contractor(String name, Date hireDate, Date contractEnds) {
    this(name, hireDate, 0, contractEnds);
}

You will need to pick some value of type long to represent a non-existent ID. I used 0.

Upvotes: 0

Related Questions