I have this generic class
public class BinTree<T> {
T value;
List<BinTree<? extends T>> branch = new ArrayList<BinTree<? extends T>>();
public BinTree(T v){ value = v;}
public void addBranch(BinTree<? extends T> tree){
if(branch.size() == 2){
System.out.println("You can only have two childs");
}else{
branch.add(tree);
}
}
public BinTree<? extends T> getBranch(int n){ return branch.get(n);}
}
And its implementation here
public static void main(String[] args){
BinTree<Number> firstBinTree = new BinTree<Number>(0);
firstBinTree.addBranch(new BinTree<Integer>(5));
firstBinTree.addBranch(new BinTree<Double>(6.5));
Number o = firstBinTree.getBranch(0).value;
firstBinTree.getBranch(0).addBranch(new BinTree<Integer>(6));
}
But this line
firstBinTree.getBranch(0).addBranch(new BinTree<Integer>(6));
Is not allowing me to add another BinTree of Type integer. why is that? I declared in my addBranch method that it can add any type as long that is ia subclass of Type(in this case number) it would be added in the list but how come I can't ? isn't Integer a sub class of Number?