18
|
super in Generics is the opposite of extends. Instead of saying the comparable's generic type has to be a subclass of T, it is saying it has to be a superclass of T. The distinction is important because extends tells you what you can get out of a class (you get at least this, perhaps a subclass). super tells you what you can put into the class (at most this, perhaps a superclass).
In this specific case, what it is saying is that the type has to implement comparable of itself or its superclass. So consider java.util.Date. It implements
Comparable . But what about java.sql.Date? It implements Comparable as well.
Without the super signature, SortedList would not be able accept the type of java.sql.Date, because it doesn't implement a Comparable of itself, but rather of a super class of itself.
| ||||||||||||
|
15
|
It's a lower-bounded wildcard.
JLS 4.5.1 Type Arguments and Wildcards
A
List , for example, includes List , List , andList .
Wildcards are used to make generics more powerful and flexible; bounds are used to maintain type safety.
See also
As to how this is useful in
, it's when you have something likeCat extends Animal implements Comparable .
Look at the signature of
Collections.sort
Therefore, with a
List , you can now Collections.sort(listOfCat) .
Had it been declared as follows:
then you'd have to have
Cat implements Comparable to use sort . By using the ? super T bounded wildcard, Collections.sort becomes more flexible.See also
| ||||||||||||||||
|
4
|
It means that
T must implement Comparable The sense is that because SortedList is sorted, it must know how to compare two classes of its genericsT parameter. That's why T must implement Comparable | ||
3
|
It means that the type
T must implement Comparable of T or one of its super classes.
For example, if
A extends B , if you want to use SortedList , A must implementComparable or Comparable , or in fact just Comparable .
This allows the list of
A s to be constructed with any valid comparator. | ||
1
|
Consider the following example:
NOTE:
Both are legal, but they are different. The first one indicates that you can pass in a ArrayList object instantiated as Animal or any Animal subtype like ArrayList, ArrayList or ArrayList. But, you can only pass ArrayList in the second, and NOT any of the subtypes.
| |||