ArrayList with different types of objects

JimS
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?

Andremoniy

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ArrayList<T> with different types for T

From Dev

Can I create an ArrayList of different types in Java

From Dev

List of pointers to different types of objects

From Dev

Interface method returns ArrayList containing different objects

From Dev

Looking for an algorithm for sorting different types of objects

From Dev

Concatenating objects of different types into a string (Python)

From Dev

AutoMapper objects with different property types

From Dev

SQL Store different types of objects in a single table

From Dev

Generic method to map objects of different types

From Dev

Editing dIfferent types of objects using different controls in Wpf Datagrid Column

From Dev

Sorting objects of different types into one list

From Dev

Filling an arraylist with Objects of two generic types

From Dev

Are reference objects with different types any different in Java?

From Dev

Efficient algorithm for ordering different types of objects

From Dev

Spawn different types of objects

From Dev

ArrayList<T> with different types for T

From Dev

Can I create an ArrayList of different types in Java

From Dev

binding different types of objects to canvas

From Dev

(Simple) Add different types to ArrayList <class>

From Dev

Creating hashsets that contain different types of objects

From Dev

Concatenating objects of different types into a string (Python)

From Dev

Efficient way to compare different types of List of Objects

From Dev

Proper way to compare two objects of different types

From Dev

Libgdx pools for different types of childs objects

From Dev

How to add different types of values into arraylist<object>?

From Dev

[Java]How to set whats in ArrayList of objects to variables of different types?

From Dev

storing custom object with different primitive types in ArrayList

From Dev

Sort arraylist which have different objects

From Dev

C# Making Array of different types of objects

Related Related

  1. 1

    ArrayList<T> with different types for T

  2. 2

    Can I create an ArrayList of different types in Java

  3. 3

    List of pointers to different types of objects

  4. 4

    Interface method returns ArrayList containing different objects

  5. 5

    Looking for an algorithm for sorting different types of objects

  6. 6

    Concatenating objects of different types into a string (Python)

  7. 7

    AutoMapper objects with different property types

  8. 8

    SQL Store different types of objects in a single table

  9. 9

    Generic method to map objects of different types

  10. 10

    Editing dIfferent types of objects using different controls in Wpf Datagrid Column

  11. 11

    Sorting objects of different types into one list

  12. 12

    Filling an arraylist with Objects of two generic types

  13. 13

    Are reference objects with different types any different in Java?

  14. 14

    Efficient algorithm for ordering different types of objects

  15. 15

    Spawn different types of objects

  16. 16

    ArrayList<T> with different types for T

  17. 17

    Can I create an ArrayList of different types in Java

  18. 18

    binding different types of objects to canvas

  19. 19

    (Simple) Add different types to ArrayList <class>

  20. 20

    Creating hashsets that contain different types of objects

  21. 21

    Concatenating objects of different types into a string (Python)

  22. 22

    Efficient way to compare different types of List of Objects

  23. 23

    Proper way to compare two objects of different types

  24. 24

    Libgdx pools for different types of childs objects

  25. 25

    How to add different types of values into arraylist<object>?

  26. 26

    [Java]How to set whats in ArrayList of objects to variables of different types?

  27. 27

    storing custom object with different primitive types in ArrayList

  28. 28

    Sort arraylist which have different objects

  29. 29

    C# Making Array of different types of objects

HotTag

Archive