Reputation: 13151
I have a confusion in 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
}
Shouldn't both of the above be valid? With the analogy of If U is parent of T , then T is child of U. Then why does 2nd one gives compilation error?
EDIT::
I think , T extends T
and T super T
both are valid. right ?
Upvotes: 8
Views: 422
Reputation: 328598
TypeParameter:
TypeVariable TypeBoundopt
TypeBound:
extends TypeVariable
extends ClassOrInterfaceType AdditionalBoundListopt
AdditionalBoundList:
AdditionalBound AdditionalBoundList
AdditionalBound
AdditionalBound:
& InterfaceType
extends
or super
(JLS #4.5.1):TypeArguments:
< TypeArgumentList >
TypeArgumentList:
TypeArgument
TypeArgumentList , TypeArgument
TypeArgument:
ReferenceType
Wildcard
Wildcard:
? WildcardBoundsopt
WildcardBounds:
extends ReferenceType
super ReferenceType
Upvotes: 7
Reputation: 1054
You can't bound a named generic with super. See also this stackoverflow posting.
Upvotes: 2