2

I am new to java language. I was reading about Generics, and came across a question.

class XXX<E extends Shape> {

}
//This class compiles , but
class XXXX<E super Shape>{

}
//This do not compile.

My question is, while defining a generic class, extends is allowed but why super is not allowed?

I been through few Generics question on stackoverflow, none answers the above question.

  • 1
    It seems to be a language restriction to make type inference easier. See these answers to a similar question: https://stackoverflow.com/a/33895470/2643425 https://stackoverflow.com/a/5671079/2643425 – Sean Van Gorder Jun 19 '17 at 18:36
  • 2
    Have you looked at what the JLS says about that? http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.1.2 http://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.1.2 http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.4. `super` is only used for wildcards. If allowed, `super` in a type variable would make it impossible to know the type of the variable, or to assign variables with a base type that's a subtype of the type argument. That renders such a type assertion useless, even harmful, were it allowed. – Lew Bloch Jun 19 '17 at 19:56

0 Answers0