• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics and type variables  RSS feed

 
Asif Pathan
Greenhorn
Posts: 16
1
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I wanted to know the reason as to why unlike wildcards, type variables must always be bounded using extends, never super that is <T super X> is not allowed while <? super X> is (where X is bound i.e. interface or class).

Following is the link where the same question is asked (did not find that earlier). But the reason (type inference) is not very clear.
http://www.coderanch.com/t/328787/java/java/Bound-Type-Variable

Any example to support the reasoning would be great.

Thanks & Regards,
Asif
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asif Pathan wrote:Hi all, I wanted to know the reason as to why unlike wildcards, type variables must always be bounded using extends, never super

Basically because, for generics, super includes Object, which isn't really of much use when you're defining a type.

On the other hand, <? super T>, must be a type in the same hierarchy as T, so if you have a parameter (which is where wildcards are normally used) defined as Whatever<? super T> - and the most common one is Comparable<? super T> - says "a Whatever whose type is in the same hierarchy as T" (or, in the case of Comparable, "something that is Comparable to something else in the same hierarchy").

So, in the first case, if T is an Integer, a Whatever<Number> will satisfy the wildcard, but a Whatever<String> won't.

HIH

Winston
 
Asif Pathan
Greenhorn
Posts: 16
1
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Winston. It seems that designers thought the benefits of allowing lower bound are outweighed by the complexity involved. I am yet to study these reasons in detail, however here is a link which discusses this issue further http://stackoverflow.com/questions/4902723/why-cant-a-java-type-parameter-have-a-lower-bound

Cheers,
Asif
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch after 6½ years.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asif Pathan wrote:I am yet to study these reasons in detail, however here is a link which discusses this issue further http://stackoverflow.com/questions/4902723/why-cant-a-java-type-parameter-have-a-lower-bound

I was unaware of that link, but through it, I found the link to Anjelika Langer's article, who is well worth reading on lots of subjects (this, for example, is part 1 of her article on equals() methods; also well worth a read). Just be warned, some of them are in German; although the best ones have English translations.

So you get a cow for pointing it out to me. Thanks.

Winston
 
Asif Pathan
Greenhorn
Posts: 16
1
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch after 6½ years.


Thanks Ritchie . I intend to make up for the lost time. Looking forward to more interesting discussions...
 
Asif Pathan
Greenhorn
Posts: 16
1
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Asif Pathan wrote:I am yet to study these reasons in detail, however here is a link which discusses this issue further http://stackoverflow.com/questions/4902723/why-cant-a-java-type-parameter-have-a-lower-bound

I was unaware of that link, but through it, I found the link to Anjelika Langer's article, who is well worth reading on lots of subjects (this, for example, is part 1 of her article on equals() methods; also well worth a read). Just be warned, some of them are in German; although the best ones have English translations.

So you get a cow for pointing it out to me. Thanks.

Winston


Thanks for the cow (very proud to have one now, good comeback... what say Ritchie? ) and suggesting the Anjelika Langer's article. I am loving the reasoning provided. Definitely on my Read List now.

Thanks again,
Asif

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!