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?
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>();
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With