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>()
;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments