Home > Enterprise >  How to add an arrayList to this child class?
How to add an arrayList to this child class?

Time:11-29

Context - So there are two classes that uses inheritance. The EmployeeService is the parent class and the EmployeeInfo is the child class.

What do I need help with - So I am trying to insert an arrayList to the parent class that combines the information of the experience and position and makes a new arrayList called serviceList.

And when I call a super() in the child class, I should be able to call the arrayList rather than the String variables (experience, position).

To put it short, I should basically be able to pass an arrayList as the third parameter in the child class employeeInfo method instead of String experience or String position

Parent class -

public class EmployeeService () {
    private String experience;
    private String position;
    
    public EmployeeService (String experience, String position) {
        this.setExperience (experience);
        this.setPosition(position);
    }
    
    public String getExperience() {
        return experience;
    }

    public void setExperience(String experience) {
        this.experience = experience;
    }
    
    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }
    
    public String toString() {
        return "Experience - "   experience   "Position"   " - "   position;
    }
    
}

Child class -

public class EmployeeInfo () {
    private String firstName;
    private String address;
    
    public EmployeeInfo (String firstName, String address,String experience, String position) {
        super(experience, position);
        this.setFirstName (firstName);
        this.setAddress(address);
    }
    
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    
    public String toString() {
        return "Name - "   firstName   "Address"   " - "   address   super.toString();
    }
}

CodePudding user response:

Just add the property and an additional constructor to the parent class as follows:

import java.util.ArrayList;
import java.util.List;

public class EmployeeService {
    private String experience;
    private String position;
    private List<String> serviceList;

    public EmployeeService (String experience, String position) {
        this.setExperience (experience);
        this.setPosition(position);
    }

    public EmployeeService (String experience, String position, List<String> serviceList) {
        this(experience, position);
        this.serviceList = new ArrayList<>(serviceList);
    }

    public String getExperience() {
        return experience;
    }

    public void setExperience(String experience) {
        this.experience = experience;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String toString() {
        return "Experience - "   experience   "Position"   " - "   position;
    }
}

Then adapt your child class:

import java.util.List;

public class EmployeeInfo extends EmployeeService {
    private String firstName;
    private String address;

    public EmployeeInfo (String firstName, String address, String experience, String position) {
        super(experience, position);
        this.setFirstName (firstName);
        this.setAddress(address);
    }

    public EmployeeInfo (String firstName, String address, String experience, String position, List<String> serviceList) {
        super(experience, position, serviceList);
        this.setFirstName (firstName);
        this.setAddress(address);
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String toString() {
        return "Name - "   firstName   "Address"   " - "   address   super.toString();
    }
}
  • Related