1

Why is this causing compilation error:

public <S super T> void addImplements(Class<S> cl)

whereas this is OK:

public <S extends T> void addImplementedBy(Class<S> cl)

T is a type parameter specified on the class. Error message on the first is Syntax error on token "super", , expected

update

This is apparently OK:

public void addImplements(Class<? super T> cl)

Which is essentially the same but without the named type S.

Why is the first variant not allowed or supported? It would appear that technically it is perfectly possible to support it. So is it invalid by design or just not supported (yet)?

I'm not getting the "doesn't buy you anything" from the linked duplicate answer. For one it buys me a named type S that I can use. The second variant (? super T) doesn't offer that.

Note same in Java7 and Java8

geert3
  • 7,086
  • 1
  • 33
  • 49
  • 2
    Possible duplicate of [Java generic methods: super can't be used?](http://stackoverflow.com/questions/8403489/java-generic-methods-super-cant-be-used) – ffff Oct 11 '15 at 17:42
  • What would be your use case for doing this ? – Gaël J Oct 11 '15 at 17:42
  • updated the question to show alternative way that does compile, although without named type `S` – geert3 Oct 11 '15 at 17:51

1 Answers1

2

The Java Language Specification for Java SE 8 defines a Type parameter with:

TypeParameter:
{TypeParameterModifier} Identifier [TypeBound]

and a Type Bound with:

TypeBound:
extends TypeVariable
extends ClassOrInterfaceType {AdditionalBound}

So the keyword super is explicitly not allowed. The reason is given by the Angelika Langer FAQ on Java Generics:

Type parameters can have several upper bounds, but no lower bound. This is mainly because lower bound type parameters of classes would be confusing and not particularly helpful.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • +1 for syntax reference. Not sure though I like "confusing" as a valid argument here (unless they mean it would confuse the parser but I don't believe that). Confusing the programmer is not a valid argument IMHO. `public >>> getStuff()` can be pretty confusing as well. I simply need to pass an implemented interface of my `T` class and label it `S`. – geert3 Oct 11 '15 at 20:51