I have just implemented some classes:
public abstract class Job<T extends ViewerUnion>{
[...]
}
public abstract class ReturnValueJob<T, S extends ViewerUnion> extends Job<S> {
[...]
}
public class MyReturnJob extends ReturnValueJob<Foo, Baa> {
[...]
}
public class JobExecuter {
public static void execute(List<Job> jobList){
for(Job actJob: jobList){
actJob.startJob();
}
for(Job actJob: jobList){
actJob.awaitTimeoutOrFinish();
}
}
}
Now I want to execute this code:
List<Job> list = new LinkedList<MyReturnJob>();
JobExecuter.execute(list);
But Java gives me some error while compiling:
Type mismatch: cannot convert from LinkedList to List
I don’t know why because the MyReturnJob is inheriting the Job.
If I change the definition of the list to LinkedList<MyReturnJob> list = new LinkedList<MyReturnJob>();
The same error is thrown on JobExecuter.execute(list);
Thanks for your help.
Best regards,
Till
What wrong here??
The problem is not the change from
LinkedListtoList, but the change in the contained type. Java generics are not covariant, which means you cannot even do:Details…
For more info, read the “Java theory and practice: Generics gotchas” article. Here its relevant part:
… applied back to context
The article example code can be translated into the context of this question like this:
where
FooJobis declared as: