1

Let's suppose I'm writing a methods which takes as parameter a list of any type of Number or its subclasses. The following signature works fine:

public void method(List<? extends Number> list) {
    (...)
}

However, the following signature also works:

public void method(List<Number> list) {
    (...)
}

Is there any case where "? extends" is really needed in method signature?

Nelio Alves
  • 965
  • 9
  • 25
  • 2
    Well you can pass `List` to `(List extends Number>)`. You can't pass `List` to `(List)` – khelwood Jun 09 '18 at 21:44
  • 2
    Really needed? No, you could simply invoke `method(new ArrayList<>(listOfInteger))` instead. But that would be ugly and inefficient. – Andy Turner Jun 09 '18 at 21:52
  • 1
    @AndyTurner That would also pass a new `List` into `method(...)`. If `method(...)` is intending to sort its input, `listOfInteger` will remain unsorted. – Kröw Jun 10 '18 at 00:08

0 Answers0