Collection<Animal> is more restrictive than Collection<? extends Animal> because Collection<Animal> matches only Animal type, but ? extends Animal matches Animal or any of its subclasses. Consider below example
Example sum method will accept List<Integer> or List<Double> or List<Number>
public static double sum(List<? extends Number> numberlist) {
double sum = 0.0;
for (Number n : numberlist) sum += n.doubleValue();
return sum;
}
Main Call sum() with List<Integer> or List<Double> will work without any issues
public static void main(String args[]) {
List<Integer> integerList = Arrays.asList(1, 2, 3);
System.out.println("sum = " + sum(integerList));
List<Double> doubleList = Arrays.asList(1.2, 2.3, 3.5);
System.out.println("sum = " + sum(doubleList));
}
But the below method will only accept List<Number>, now if you try to call passing List<Integer> or List<double> you will have Compile time error
public static double sum(List<Number> numberlist) {
double sum = 0.0;
for (Number n : numberlist) sum += n.doubleValue();
return sum;
}
CE
The method sum(List<Number>) in the type NewMain is not applicable for the arguments (List<Double>)
The method sum(List<Number>) in the type NewMain is not applicable for the arguments (List<Integer>)