leithdm
4/14/2020 - 7:22 PM

Wildcards....two ways to write the same thing

Wildcards....two ways to write the same thing

    //wildcards to the rescue ! note that we cannot add to the array...compiler wont let us
    public void takeArrayAnimalsWild(ArrayList<? extends Animal> arrayAnimals) {
        for (Animal a : arrayAnimals) {
            a.eat();
            //arrayAnimals.add(new Cat());     //!* This is the reason why we cant pass in an ArrayList<Dog> to ArrayList<Animals>
        }
    }

    //this is another way of writing the exact same thing above
    public <T extends Animal> void takeArrayAnimalsWild(ArrayList<T> list) {