I have an ArrayList which contains objects of the super class and some subclass objects. Let's call them subclass1 and subclass2.
Is there a way I can go ArrayList and discern which objects are SuperClass, subclass1 and subclass2. So I can put them into ArrayList and ArrayList.
This is an overly simplified version but it demonstrates what I'm hoping to do.
public class food{
private String name;
public food(String name){
this.name = name;
}
}
public class pudding extends food{
public pudding(String name){
super(name);
}
}
public class breakfast extends food{
public breakfast(String name){
super(name);
}
}
public static void main(String args[]){
ArrayList<food> foods = new ArrayList();
foods.add(new food("Sausage"));
foods.add(new food("Bacon"));
foods.add(new pudding("cake"));
foods.add(new breakfast("toast"));
foods.add(new pudding("sponge"));
foods.add(new food("Rice"));
foods.add(new breakfast("eggs"));
ArrayList<pudding> puds = new ArrayList();
ArrayList<breakfast> wakeupjuices = new ArrayList();
for(food f : foods){
//if(f is pudding){puds.add(f);}
//else if(f is breakfast){wakeupjuices.add(f);}
}
}
instanceof