Not <U, T extends U> and <T, U super T> the same?

I have confusion in the following two method declarations:

private <U, T extends U> T funWorks(T child, U parent) { // No compilation errors } private <T, U super T> T funNotWorks(T child, U parent) { // compilation errors } 

Should both of the above be valid? By analogy with If U is the parent of T, then T is a child of U. Then why does the second give a compilation error?

EDIT :: I think T extends T and T super T both valid. right?

+8
java collections generics type-parameter
source share
2 answers
  • Type parameters (your example) can only use extends ( JLS # 4.4 ):
 TypeParameter: TypeVariable TypeBoundopt TypeBound: extends TypeVariable extends ClassOrInterfaceType AdditionalBoundListopt AdditionalBoundList: AdditionalBound AdditionalBoundList AdditionalBound AdditionalBound: & InterfaceType 
  • Wildcards can use either extends or super ( JLS # 4.5.1 ):
 TypeArguments: < TypeArgumentList > TypeArgumentList: TypeArgument TypeArgumentList , TypeArgument TypeArgument: ReferenceType Wildcard Wildcard: ? WildcardBoundsopt WildcardBounds: extends ReferenceType super ReferenceType 
+7
source share

You cannot associate named generic with super. See also this stackoverflow posting.

+2
source share

All Articles