mogronalol
mogronalol

Reputation: 3015

Allowing subclasses to be added to a list with Generics in Java

I do not understand why the compiler does not allow this:

Callable callable = null;
List<Future<BaseClass<? extends AnotherClass>> futures = new ArrayList<Future<BaseClass<? extends AnotherClass>>>();

class BaseClass<T extends AnotherClass> {
   ...
}


class Subclass extends BaseClass<ConcreteType> {
    ...
}

class ConcreteType extends AnotherClass {
    ...
}

futures.add(new FutureTask<Subclass>(callable));

Why is this not allowed?

Upvotes: 0

Views: 103

Answers (1)

jtahlborn
jtahlborn

Reputation: 53694

I believe you want:

List<Future<? extends BaseClass<? extends AnotherClass>>> futures

Upvotes: 2

Related Questions