JimS
JimS

Reputation: 349

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?

Upvotes: 3

Views: 3032

Answers (5)

Loco234
Loco234

Reputation: 521

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);

    }
}

Upvotes: 2

Nirmal
Nirmal

Reputation: 1259

The initialization like below to add to list.

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

To read from the list later, you could use instanceof and cast to the known type that you have in your code.

Upvotes: -3

SimonPJ
SimonPJ

Reputation: 766

Create an Employee class that these two can inherit from and use that in the ArrayList

public abstract class Employee 
{
    public string setName(string name) { ... }
    public string setAfm(string afm) { ... }
    public int setSalary(float afm) { ... }
}

public class HourlyEmployee extends Employee
{
}

public class SalariedEmployee extends Employee
{
}

ArrayList<Employee> employees;

This will also mean you will not have to rewrite your setter methods for each employee class

Upvotes: 1

Abdul Ahmad
Abdul Ahmad

Reputation: 10021

Why don't you make a superclass "employee" and make both objects children of employee:

class Employee { }
class SalariedEmployee extends Employee { }
class HourlyEmployee extends Employee { }

Employee salariedEmp = new SalariedEmployee();
Employee hourlyEmp = new HourlyEmployee();

Then create an array list of type Employee:

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

Upvotes: 0

Andremoniy
Andremoniy

Reputation: 34900

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>();

Upvotes: 5

Related Questions