Glide
Glide

Reputation: 21245

Java generics help

I have class A and a class B that extends A.

I have an abstract class X and a concrete class Y that extends X.

In X I have an abstract method with the following signature:

public abstract Collection<? extends A> getStuff();

In Y I implement the abstract method with the following signature:

public Collection<B> getStuff();

When I try to do the following: classX.getStuff().add(B)

It gave the following complaint: add (capture ) in Collection cannot be applied to B

Any help would be appreciated.

Upvotes: 0

Views: 128

Answers (1)

jumperchen
jumperchen

Reputation: 1473

You can use the following example as your X class.

For example,

public abstract <T extends A> Collection<T> getStuff();

You can also refer to this threads.

Upvotes: 1

Related Questions