2
Hello, I have the following task to do:
I have three types of employees (Commissioned, Salaried and Hourly) and I have to add them to a list (ArrayList) to manage them.
ArrayList<Commissioned> c = new ArrayList<>();
ArrayList<Hourly> h = new ArrayList<>();
ArrayList<Salaried> s = new ArrayList<>();
I need them all to be in a single Array. I could create a Employees interface, implement Employees in each of the classes and make a
ArrayList<Employees> employees = new ArrayList<>();
but that’s not what I need.
What I have to do is
ArrayList<Objects> employees = new ArrayList<>();
But I don’t know how to work with these employees in an Array of Objects. How to use things like instanceof or casting of objects.
What I wish I could do:
Find out if an item of this ArrayList is an instance of the class Commissioned, Salaried or Hourly. And how to use the methods of each of the array items, since the methods of each class are different.
Note: If you want to see how the project is currently being implemented: https://github.com/WilliamPhilippe/Projeto-de-Software/tree/master/FolhadePagamentos/src/com/company
- This in the Working Line 11 class
Obs2: I searched a lot on the Internet, but I couldn’t find things about Objects Array, just about Objects Array.
Willian Philippe: The question has some answers. Have they been helpful to you? If they have helped you consider accepting one of them. Read What should I do if someone answers my question? and How and why to accept an answer?.
– Augusto Vasques