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 subclass of Foo
. The return type of the method 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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With