3

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.

1 Answer 1

11

Use extends instead of implements.

Sign up to request clarification or add additional context in comments.

4 Comments

... 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 ...
+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.
@Andreas: no, it doesn’t work only on wildcards, it also works on template arguments.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.