[Logo]
Forums Register Login
Why is it possible to say <T extends Integer> even though Integer is marked as final?
When reading the topic called"Bounded Type Parameters", I saw it is saying <T extends Integer> ,which recalls me that Integer is marked final. So I feel confused.
Especially it emphasizes "  Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces)." and "A type variable with multiple bounds is a subtype of all the types listed in the bound. "(see the pictures).They come from The Java™ Tutorials.
https://docs.oracle.com/javase/tutorial/java/generics/bounded.html
extends.PNG
[Thumbnail for extends.PNG]
definition.PNG
[Thumbnail for definition.PNG]
final.PNG
[Thumbnail for final.PNG]
add one more picture.
subtype.PNG
[Thumbnail for subtype.PNG]
Start by remembering that for the purposes of generics, every type is a supertype of itself and every type is a subtype of itself. So, <T super Integer> includes Object, Number, and Integer, and <T extends Integer> includes only Integer.
You are defining a formal type parameter inside a generic class. What it means is that the NaturalNumber class there has a type parameter T, but you won't allow that to be a String or a Double or even a general Number. The only way you can restrict it to a particular type is to us <T extends Foo> as its formal type parameter. As you will see from this discussion, you cannot simply write <Integer> as a formal type parameter and have the class accept only Integers.
Thank you very much for your reply.
I understand one of benefits about bounded type parameter is to restrict its type choice.It seems to make sense only when explaining that <T extends Integer> means it restricts T to be Integer only considering that Integer is a final class. However,It appears to violate rules for extending a final class.Maybe can I say that some description or definition is not thorough enough?
It seems that the rule says A cannot do something.When
the fact that A did something occurred,in order to make sense, we have to give it another explanation.
It seems that the rule says A cannot do something.When
the fact that A did something occurred,in order to make sense, we have to give it another explanation.
(1 like)
"final" for a class means that you cannot define a subclass based on it. But a generic is not the same thing as a subclass.
In my opinion, the Java Language Specification is complex enough as it is -- to add more checks that yields little is a bit silly.

The reason the <T extends Integer> generic is allowed is because it is possible to find a T that satisfies the bound. And even it can't (as with some interfaces) it can only do the checks as specified.

To add a check that if you find a T, and that T is final, and the bound is not an interface, etc. then it should be an error -- well, like I said the JLS is already complex enough as it is. It may be added in the future, but I wouldn't hold my breath.

... and also, how would the JLS word it? Yes, it is a valid bound, but since there is only one possible case, due to "final", this is inefficient, so, it will be an error (or warning)?

Henry
It would be very sad if your generic method didn't work for subtypes if the developer of the type suddenly decided they wanted to remove the final modifier.

This thread has been viewed 1853 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 17, 2018 22:34:42.