• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparable and generics  RSS feed

 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

How is it logically correct to compare a sub class with a super class? I read in a book that -
It isn�t necessary that T be comparable to exactly itself. All that�s required is that T be comparable to one of its supertypes.

and that is why comparable is defined as
Comparable<? super T>

Thanks
 
Joe Ess
Bartender
Posts: 9439
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rakesh sugirtharaj:
How is it logically correct to compare a sub class with a super class?


Would it make sense to compare two different ways to represent the same thing, like a date on the Gregorian Calendar to a date on the Julian Calendar? I'd think so. Presumably they'd have the same base class and that would indicate the relationship between the derived classes: they both represent time.
Originally posted by rakesh sugirtharaj:

Comparable<? super T>


I don't see it. Perhaps your book does that to drive the point home?
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually found that statement in a pdf download from Sun site.
Generics tutorial by Gilad Bracha.
 
Daniel Chemko
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You sparked my interest in what the differences would really be, so I wrote a test app to play around with the concept. Anywhere the file doesn't compile, we can see the limitations of each type of passing in a comparator

 
Dr Cheaper
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also compare compare generics.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Dr Cheaper",

Please read your private messages regarding an important announcement.

Thank you,

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