tags:

views:

99

answers:

2

I've got a class Foo<T>. How can I say that I want T to be some class implementing BarInterface? Writing simply class Foo<T implements BarInterface> doesn't compile.

+8  A: 

Use extends instead of implements.

Konrad Rudolph
... if I had a compiler at hand I could test it right away: shouldn't it be `class Foo<? extends BarInterface>` - `extends` is a [WildcardBound](http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.5.1) ...
Andreas_D
+1 One note to add: Extends in the context of Generics has both the meaning of 'extends' AND 'implements', which I found quite confusing in the beginning.
Helper Method
@Andreas: no, it doesn’t work only on wildcards, it also works on template arguments.
Konrad Rudolph
@Konrad - thanks for clarification. I was really unsure, my first intention was that it `<T extends Something>` is correct but I was quite irritated after reading this one chapter of the language spec.
Andreas_D
A: 

Have a look at http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf, 4.1 bounded wildcards

davyM