Reputation: 18468
Can someone explain what's the difference between these two snippets of code ?
1)
private Collection<Animal> getAnimal() {
return null;
}
2)
private Collection<? extends Animal> getAnimal() {
return null;
}
I understand that ?
is a wildcard and I can use anything instead of it. Then I specify extends which bound that wildcard to Animal but in that case isn't the first example the same as second ? What's the difference ?
Thanks
Upvotes: 1
Views: 98
Reputation: 40048
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>)
Upvotes: 3