I'm going to make a method that return ArrayList of multiple Object, like this:
public ArrayList<Object> getData(Object similar) {
//I suppose that I have 2 ArrayList that already contain data here
ArrayList<Human> humans = new ArrayList<Human>();
ArrayList<Animal> animals = new ArrayList<Animal>();
if (similar.getClass().equals(Human.class)) {
return humans;
}
if (similar.getClass().equals(Animal.class)) {
return animals;
}
return null;
}
My idea is check type of Object param, if it's Human class then return ArrayList of Human. But I can't return like that because of incompatible type.
The problem is the return object is not specific.
How do I fix it? Thank you!