I am building a generic Tree
class, which supports inheritance of sub-trees. But I\'ve encountered some problems. Would you please kindly help me?
did you try such code?
package trees;
import java.util.ArrayList;
public class Trees {
public static void main(String... args) {
Tree> tree_leaf = new Tree<>();
BlueTree> blueTree_leaf = new BlueTree<>();
Tree> tree_redLeaf = new Tree<>();
BlueTree> blueTree_redLeaf = new BlueTree<>();
//1
tree_leaf.addChild(tree_leaf);
tree_leaf.addChild(tree_redLeaf);
tree_leaf.addChild(blueTree_leaf);
tree_leaf.addChild(blueTree_redLeaf);
//2
tree_redLeaf.addChild(tree_redLeaf);
tree_redLeaf.addChild(blueTree_redLeaf);
tree_redLeaf.addChild(tree_leaf);//compile error
tree_redLeaf.addChild(blueTree_leaf);//compile error
//3
blueTree_leaf.addChild(blueTree_leaf);
blueTree_leaf.addChild(blueTree_redLeaf);
blueTree_leaf.addChild(tree_leaf);//compile error
blueTree_leaf.addChild(tree_redLeaf);//compile error
//4
blueTree_redLeaf.addChild(blueTree_redLeaf);
blueTree_redLeaf.addChild(tree_leaf);//compile error
blueTree_redLeaf.addChild(tree_redLeaf);//compile error
blueTree_redLeaf.addChild(blueTree_leaf);//compile error
}
}
class Tree> {
//important in this question
private Tree super Data, ? super Children> mParent;
private Data mData;
private ArrayList mChildren;
// This is the focus of this question, the addChild() method signature
public void addChild(final Children subTree) {
// add the subTree to mChildren
}
}
class BlueTree> extends Tree {
}
class Leaf {
}
class RedLeaf extends Leaf {
}