public <E extends Foo> List<E> getResult(String s);
where Foo
is my own class.
What is the return type of this method? Why does it seem to have two return types?
public <E extends Foo> List<E> getResult(String s);
where Foo
is my own class.
What is the return type of this method? Why does it seem to have two return types?
No, you don\'t have two return types.It\'s a generic method you are seeing.
<E extends Foo> --> you are declaring a generic type for your method
List<E> --> this is your return type
Your method can have a generic type E
which is a sub-class of Foo
. your return type is a List<Foo or any SubType Of FOO>
The return type is List<E>
. The clause <E extends Foo>
is not a return type; it is a generic type declaration, specifying that the particular type E
must be a Foo
(or a subclass of Foo
). This is standard syntax for declaring a generic method.
Take a look at the Java documentation pertaining to generics.
<E extends Foo> // declares the bounds for the generic type `E`
List<E> // declares the return value
The return type of the method is List<E>
.