• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is it possible to say <T extends Integer> even though Integer is marked as final?  RSS feed

 
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Crystal Zeng
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add one more picture.
subtype.PNG
[Thumbnail for subtype.PNG]
 
Marshal
Posts: 58463
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Crystal Zeng
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Crystal Zeng
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Crystal Zeng
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 19273
85
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
author
Marshal
Posts: 23441
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 8623
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!