Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is <? super T> syntax? [duplicate]

Tags:

java

I'm having trouble understanding the following syntax:

public class SortedList< T extends Comparable< ? super T> > extends LinkedList< T > 

I see that class SortedList extends LinkedList. I just don't know what

T extends Comparable< ? super T> 

means.

My understanding of it so far is that type T must be a type that implements Comparable...but what is < ? super T >?

like image 672
ShrimpCrackers Avatar asked May 13 '10 14:05

ShrimpCrackers


People also ask

What is <? Super T in Java?

super T denotes an unknown type that is a supertype of T (or T itself; remember that the supertype relation is reflexive). It is the dual of the bounded wildcards we've been using, where we use ? extends T to denote an unknown type that is a subtype of T .

What is the difference between List <? Extends T and List <? Super T >?

super is a lower bound, and extends is an upper bound.

What is super object in Java?

Definition and Usage. The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.

What is wildcard in Java generic?

The question mark (?) is known as the wildcard in generic programming. It represents an unknown type. The wildcard can be used in a variety of situations such as the type of a parameter, field, or local variable; sometimes as a return type.


1 Answers

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<Date>. But what about java.sql.Date? It implements Comparable<java.util.Date> 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.

like image 92
Yishai Avatar answered Nov 23 '22 23:11

Yishai