0

i am learning generics.

What is the difference between these two notations?

public class A<Key extends Comparable<? extends Key>>

public class A<Key extends Comparable<? super Key>>

I understand difference between extend and super wildcard generics, but what does it means when used with comparable?

user2565192
  • 694
  • 1
  • 8
  • 19
  • Second one makes no sense – Grim Jun 13 '16 at 18:18
  • @PeterRader, it's the first one that makes no sense...you always want `Comparator super Whatever>`. – Louis Wasserman Jun 13 '16 at 18:30
  • @LouisWasserman `super` means that even Object is allowed but something inherit from `Key` is not allowed. Why do you think `super` make sense? – Grim Jun 13 '16 at 18:37
  • It makes sense for `Comparable`, but not necessarily other types. `Comparable` means the type is comparable to Whatevers. `Foo extends Comparable` is useless, because you can't even compare `Foo` to other `Foo`s, but `SubFoo extends Comparable` means you can compare a `SubFoo` to not just other `SubFoo`s, but `Foo`s as well. `Foo extends Comparable` means a `Foo` can be compared to anything. – Louis Wasserman Jun 13 '16 at 18:39
  • That's why, for example, [`Collections.sort`](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List)) has the constraint `>`. If you tried to write it the other way it couldn't work. – Louis Wasserman Jun 13 '16 at 18:40

0 Answers0