Hi, I am wondering if there is a simple solution to my problem,
I have an ArrayList :
ArrayList <Animal> animalList = new ArrayList<Animal>(); animalList.add(new Reptile()); animalList.add(new Bird()); animalList.add(new Amphibian());
They all implement the move() method - Bird flies when move() called. I know that I can access the general methods and properties of a superclass using this
public void feed(Integer animalIndex) { Animal aAnimal = (Animal) this.animalList.get(animalIndex); aAnimal.eat(); }
This is good - but now I would like to access the move() method, which has a subclass of Bird . I could do this by typing Animal as Bird :
Bird aBird = (Bird) this.animalList.get(animalIndex); aBird.move();
In my situation, I do not want to do this, as this will mean that I have 3 different sets of code above for each subtype of Animal .
Seems a little redundant, is there a better way?
source share