1

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

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
Martin Čuka
  • 16,134
  • 4
  • 23
  • 49
  • 1
    PECS -- [producer extends, consumer super](https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super) Basically, you can take an `Animal` out of the second one, but you can't put any type of object into it. If that's not the intended use model, then use the first one. – markspace Feb 11 '19 at 06:10

1 Answers1

3

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>)
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98