Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 432
Kiran Avatar asked Jun 08 '26 16:06

Kiran


1 Answers

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.

like image 96
Louis Wasserman Avatar answered Jun 11 '26 10:06

Louis Wasserman