geert3
geert3

Reputation: 7341

Unexpected syntax error on method type parameter

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

Upvotes: 1

Views: 740

Answers (1)

Ortomala Lokni
Ortomala Lokni

Reputation: 62635

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.

Upvotes: 2

Related Questions