Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList with different types of objects

public static void main (String[] args)
{
  SalariedEmployee se = new SalariedEmployee();
  HourlyEmployee he = new HourlyEmployee();
  se.setName("Simos");
  se.setAfm("111440000");
  se.setSalary(4500);
  he.setName("Chatzis");
  he.setAfm("011155555");
  he.setHoursWorked(200);
  he.setHourlyPayment(25);
  ArrayList list = new ArrayList();
  list.add(se);
  list.add(he);
}

So I have two objects of different types and I want to add them to a list. How can I make it so that it's safe compiler-wise. Since the objects were created from different classes I cannot use generics when making the list. Or can I change the type of the list after I made it. I mean can I have this

ArrayList<SalariedEmployee> list = new ArrayList<SalariedEmployee>();

add "se" object of SalariedEmployee and then change the generic to HourlyEmployee and then add the "he" object of HourlyEmployee?

like image 710
JimS Avatar asked Jun 26 '26 20:06

JimS


2 Answers

It looks like you can create common interface Employee and make both classes implement it. So you will be able to use generics:

List<Employee> list = new ArrayList<Employee>();

like image 146
Andremoniy Avatar answered Jun 28 '26 10:06

Andremoniy


Creating the interface:

Employee.java

public interface Employee {
    public void setName(String name);
    public String getName();

    public void setAfm(String afm);
    public String getAfm();

}

Then create the two classes implementing it, adding their unique methods:

HourlyEmployee.java

public class HourlyEmployee implements Employee {

    private String name;
    private String afm;
    private int hoursWorked;
    private int hourlyPayment;

    @Override
    public void setName(String name) {
        this.name = name;

    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public void setAfm(String afm) {
        this.afm = afm;
    }

    @Override
    public String getAfm() {
        return this.afm;
    }

    public int getHoursWorked() {
        return this.hoursWorked;
    }

    public void setHoursWorked(int hoursWorked) {
        this.hoursWorked = hoursWorked;
    }

    public int getHourlyPayment() {
        return this.hourlyPayment;
    }

    public void setHourlyPayment(int hourlyPayment) {
        this.hourlyPayment = hourlyPayment;
    }
}

SalariedEmployee.java

public class SalariedEmployee implements Employee {

    private String name;
    private String afm;
    private int salary;

    @Override
    public void setName(String name) {
        this.name = name;

    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public void setAfm(String afm) {
        this.afm = afm;

    }

    @Override
    public String getAfm() {
        return this.afm;
    }

    public int getSalary() {
        return this.salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

}

Then to test:

import java.util.ArrayList;

public class TestMain {

    public static void main(String[] args){
         SalariedEmployee se = new SalariedEmployee();
          HourlyEmployee he = new HourlyEmployee();
          se.setName("Simos");
          se.setAfm("111440000");
          se.setSalary(4500);
          he.setName("Chatzis");
          he.setAfm("011155555");
          he.setHoursWorked(200);
          he.setHourlyPayment(25);
          ArrayList<Employee> list = new ArrayList<Employee>();
          list.add(se);
          list.add(he);

    }
}
like image 35
Loco234 Avatar answered Jun 28 '26 08:06

Loco234