Search code examples
javagenericscollectionssubtyping

What is the difference between "? extends E" and "T extends E"?


I am new to java and am trying to understand the curious syntax below from Java Generics and Collections book.. (I worked extensively with C++ templates and hence can claim to understand the basics of generic programming and the probable gotchas):

interface Collection <E> {
  ...
  public boolean addAll(Collection<? extends E> c);
  ...
}

Why can't the above be written as:

interface Collection <E> {
  ...
  public boolean addAll(Collection<T extends E> c);
  ...
}

What is the difference? Is it just the language restriction or is there any difference under the hood?


Solution

  • It could be written as

     public <T extends E> boolean addAll(Collection<T> c)
    

    but there would be no point. There's no need to name that parameter.