90

I am trying to learn Java generics. I am not clear when you will use <T extends Foo> and when you will use <T super Foo>. What do each one of these things mean about T? Lets say I have <T extends Comparable> and <T super Comparable>, what do each of these mean?

I've read several tutorials at sun.com but I'm still lost. Can somebody illustrate with examples?

Lii
  • 11,553
  • 8
  • 64
  • 88
benhsu
  • 5,346
  • 8
  • 39
  • 47
  • This has been discussed before - see http://stackoverflow.com/questions/1368166/what-is-a-difference-between-super-e-and-extends-e and http://stackoverflow.com/questions/252055/java-generics-wildcards and http://stackoverflow.com/questions/1906677/super-e-and-extends-e-for-list – Adrian Dec 15 '09 at 22:50
  • (Note `Comparable` is a generic type, so using it as a raw type in a generic bound will cause misery. It should, at the very "simplest" be `Comparable>`.) – Tom Hawtin - tackline Dec 15 '09 at 23:00
  • 6
    there is no "", only " super Whatever>" – newacct Dec 15 '09 at 23:45

6 Answers6

296

It depends which way on the inheritance hierarchy it allows. Assume you have a class "Child" which inherits from "Parent" which inherits from "Grandparent".

<T extends Parent> accepts either Parent or Child while <T super Parent> accepts either Parent or Grandparent.

Jeffrey Martinez
  • 4,384
  • 2
  • 31
  • 36
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
18

There are three types of wildcards:

  • ? extends Type: Denotes a family of subtypes of type Type. This is the most useful wildcard.
  • ? super Type: Denotes a family of supertypes of type Type.
  • ?: Denotes the set of all types or any.
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
Ivo Bosticky
  • 6,338
  • 6
  • 34
  • 35
  • 25
    FYI: this is extracted from http://java.sun.com/developer/technicalArticles/J2SE/generics/ In the future please mention your sources instead of writing as if they're yours. – BalusC Dec 15 '09 at 23:52
  • 1
    @BalusC's link to the article doesn't work anymore. Here's a current one: http://www.oracle.com/technetwork/articles/javase/generics-136597.html – Dathan Aug 09 '13 at 02:32
15

For me the best answer came from @BSingh, when i read the article of Uncle Bob. I resume here, the conclusion of article.

Use List< T super Suit> whenever you are going to write into the list.

When you put an Object to the List, all you care about is that the object is of a type that is compatible with type held by the list. So you want the list to take the type of that object or any of the superclasses of that object.

Use List< T extends Suit> whenever you are going to read from a list.

On the other hand, when you read from a list, you want the type you are reading to be the type contained byt the list, or a derivative from that type.

15

See Effective Java 2nd Edition, Item 28:

PECS

Producer extends, Consumer super

If your parameter is a producer, it should be <? extends T>, if it's a consumer it has to be <? super T>.

Take a look at the Google Collections, they know how to use it, because they got Bloch ;)

whiskeysierra
  • 5,030
  • 1
  • 29
  • 40
  • 60
    This doesn't really explain how they work or what they mean, whereas [R Samuel Klatchko's answer](http://stackoverflow.com/a/1911036/254477) does. – Sled Nov 26 '12 at 18:22
  • 12
    Dont get it, doesn't explain the difference. – Basil Musa Dec 15 '12 at 18:02
  • 9
    This is how Bloch explains it: _Suppose you want to add bulk methods to_ `Stack`: •`void pushAll(Collection extends E> src);` •`void popAll(Collection super E> dst);` • _User can_ `pushAll` _from a_ `Collection` _or a_ `Collection` _onto a_ `Stack` • _User can_ `popAll` _into a_ `Collection` _or a_ `Collection` _from a_ `Stack` – 18446744073709551615 Mar 12 '13 at 09:12
  • 2
    Producer? Consumer? Please explain. You're not answering the question. – Patrick Garner Jul 22 '14 at 22:49
  • 2
    It's nice that you like quoting Effective Java 2nd Edition, but the author has already mentioned he's searched around, implying he has seen this already. Anyway, his question wasn't when to use super vs extends, but what they actually mean. This implies he's looking for the mechanical difference between the two to gain understanding of the 'why' to use rather than the 'when' to use. This answer is at best tangential, but I think it's, rather, completely irrelevant to the question. – josiah Apr 08 '15 at 16:40
5

If you are asking about type parameters, then there is no <T super X> construct in Java. Bounded parameter can only extend, but it can extend more than one type. E.g

public class MyClass< T extends Closeable & Runnable >
{
  // Closeable and Runnable are chosen for demonstration purposes only
}

In this case if you see MyClass< ConcreteT > then ConcreteT must be declared as

public class ConcreteT
  implements Closeable, Runnable
{
   ...
}

For bounded wildcards, read this article. Read section on get-put principle. Basically, super corresponds to write semantics, and extends corresponds to read semantics.

Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
3

Remember PECS - Producer Extends Consumer Support. Also, uncle Bob discusses it well in his craftsman series. Check out http://objectmentor.com/resources/articles/The_Craftsman_44__Brown_Bag_I.pdf

BSingh
  • 451
  • 1
  • 6
  • 9