I have the following structure. A parent class:
public class MyTree<E extends MyTree<E>>{
List<E> children;
public MyTree(List<E> ls){
this.children = ls;
}
public void insert(E t){
this.children.add(t);
}
}
a child class:
public class SpecTree<E> extends MyTree<SpecTree<E>>{
private E value;
public SpecTree(E value){
super(new ArrayList<>());
this.value = value;
}
}
Now from main, I want to insert a tree into another tree.
SpecTree<Number> st = new SpecTree<>(0);
st.insert(new SpecTree<Integer>(2)); //incompatible type error
The SpecTree
must be able to accept new children with values that are subtype of the current tree. For example, I should be able to insert a SpecTree<Integer>
into SpecTree<Number>
.
Is it possible to do without changing the structure of code and changing only the type parameters?