Here is my Java Code:
package com.Pramithas.Project;
import java.util.ArrayList;
import java.util.List;
abstract class Animal{
public abstract void check();
}
class Dog extends Animal{
public void check(){
System.out.println("From inside Dog");
}
}
public class AnimalDoctor {
public void checkAnimals(List<? super Dog>animals){
animals.add(new Dog());
for(Animal a:animals){
// Here is the problem. I also tried (Dog d:animals),
//but it did not work either.
}
}
public static void main(String[] args) {
List<Animal>myList=new ArrayList<Animal>();
myList.add(new Dog());
myList.add(new Dog());
new AnimalDoctor().checkAnimals(myList);
}
}
Inside the method checkAnimals() I am trying to access the contents of the list animals
using enhanced for statement
. But having the problem. Please help me out.