jarz
jarz

Reputation: 732

Generic Return Type in Java

I'm implementing a method on an abstract class which returns a list of concrete types depending on what parameter it receives.

public static List<T extends AbstractClass> method(List<? extends AbstractClass> objects) {
    return listOfT;
}

The method above lives on the abstract class. If I call it with a list of ConcreteType1 then I get back a List<ConcreteType1>. If I call it with ConcreteType2, then I get back a List<ConcreteType1>.

As it's written it doesn't work right now. I get a compilation error that says "Unexpected Bound". Any ideas?

Upvotes: 1

Views: 1213

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198033

This should be written

public static <T extends AbstractClass> List<T> method(List<T> objects) {
    return listOfT;
}

Upvotes: 2

Related Questions