I'd like to ask about java type erasure rules.
If we have classes:
public class Shape{}
public class Circle extends Shape{}
public class Base<T extends Shape>{
T x;
public void setX(T t){}
}
public class MainClass(){
public static void main(String... _arg){
Base<? extends Shape> bs = new Base<Circle>();
bs.setX(new Circle()); // <- compilation problem
}
}
Can you please explain me why calling setX() method causes compilation problem?
? super Shape
would work there. "Producer Extends, Consumer Super" – Michael Myers♦ 2 days agoBase<? super Shape>
accepts aBase
of any supertype ofShape
. – Oli Charlesworth 2 days ago