I'm working on this piece of code:
List<Class<? extends Foo>> models = new ArrayList<>();
if (bar == 0)
models = getRandomBlas(); // returns List<Bla>
else
models = getRandomBlubs(); // returns List<Blub>
with:
public class Bla extends Foo { ... }
public class Blub extneds Foo { ... }
But I'm getting an Incompatible types: Found: 'java.util.List<Bla>', required: 'java.util.List<java.lang.Class<? extends Foo>'
Has anyone got an idea? To my knowledge, this is how the ? extends
should work...
You're right. That is how ? extends
works, but your list wants Class
objects.
List<Class<? extends Foo>>
can contain Class
objects that represent subtypes of Foo
(Foo.class
, Bla.class
, Blub.class
).
List<? extends Foo>
can contain objects that are subtypes of Foo
.
You want the second version.