I can't wrap my head around, why the following code snippet including generics and wildcards is not valid.
package test;
public class App {
private interface Condition {
String get();
}
private interface Processor<T extends Condition> {
boolean process(final T condition);
}
private static class HolderClass<T extends Condition> {
private final Processor<T> processor;
private final T condition;
public HolderClass(final Processor<T> processor, final T condition) {
this.processor = processor;
this.condition = condition;
}
public Processor<T> getProcessor() {
return processor;
}
public T getCondition() {
return condition;
}
}
private static class FirstCondition implements Condition {
@Override
public String get() {
return "concrete";
}
}
private static class FirstProcessor implements Processor<FirstCondition> {
@Override
public boolean process(FirstCondition condition) {
System.out.println(condition.get());
return false;
}
}
public static void main(String[] args) {
final HolderClass<? extends Condition> holder = new HolderClass<>(new FirstProcessor(), new FirstCondition());
holder.getProcessor().process(holder.getCondition()); // error here
}
}
This fails with Compilation failure .../test/App.java:[58,46] incompatible types: test.App.Condition cannot be converted to capture#1 of ? extends test.App.Condition
I already have read a lot about type erasure, but can anyone try to explain what's the actual problem and maybe provide an counter example why this is not allowed?
Thanks.