I found I can't call generic methods of wildcard types and don't understand why?
public class GenericsTry2 {
public static class Element {
private Container<? extends Element> container;
public Container<? extends Element> getContainer() {
return container;
}
public void setContainer(Container<? extends Element> container) {
this.container = container;
}
public void doStuff() {
getContainer().doStuff(this); // how to call this?
}
}
public static class SomeSubElement extends Element {
}
public static class SomeSubElement2 extends Element {
}
public static class Container<E extends Element> {
public void doStuff(E element) {
}
}
public static void main(String[] args) {
Container<SomeSubElement2> c = new Container<SomeSubElement2>();
Element e = new SomeSubElement();
c.doStuff((SomeSubElement2) e); // still can do this at compile time this way
}
}