Is it possible to put classes with the same superclass in an ArrayList and that I can retrieve the fields of the subclass?
public class Animal {
private String name;
public String getName() { ... }
}
public class Dog extends Animal {
private String tailLength;
public String getTailLength() { ... }
}
public class Bird extends Animal {
private String beakSize;
public String getBeakSize() { ... }
}
Now I'd like to put them in an arrayList
private List<Animal> animals = new ArrayList<>();
animals.put(new Dog());
animals.put(new Bird());
I'm able successfully put them in the list, but when I retrieve the value from the list, I'm not able to get the beakSize from the bird class and the tailLength from the dog class.
Can you suggest of a strategy how to do this? Thanks!