Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the generic bound "E implements I" result in a compiler error?

Tags:

java

generics

Why does this compile:

class MaxMin<E extends Comparable<E>>
{
   E max=null;
   E min=null;
}

...but this doesn't?

class MaxMin<E implements Comparable<E>>
{
   E max=null;
   E min=null;
}
like image 207
achyut Avatar asked Nov 06 '11 18:11

achyut


1 Answers

Generic type bounds only specify extends and super.

Quoting the Java Generics Tutorial(emphasis mine)

To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in this example is Number. Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces)

like image 153
Sahil Muthoo Avatar answered Sep 20 '22 20:09

Sahil Muthoo