I want to list.addAll, but get a error, what should I do?
The code as follow
public class Animal {
private void speak(){
System.out.println("Animal");
}
protected void name(){
System.out.println("name");
}
}
public class Dog extends Animal{
public void test(){
super.name();
speak();
}
private void speak(){
System.out.println("dog");
}
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.test();
List<? extends Animal> animals = new ArrayList<>();
List<Dog> dogs = new ArrayList<>();
dogs.add(new Dog());
dogs.add(new Dog());
//TODO this is error
animals.addAll(dogs);
}
}
Required type: Collection <? extends capture of ? extends Animal>.
Provided: List< Dog >.