I have an issue with a Tree we have implemented, here's a sample:
public interface TreeNode {
TreeNode getParent();
void setParent(TreeNode parent);
List<TreeNode> getChildren();
void setChildren(List<TreeNode> children);
}
So, this is easy enough until now, but we have some variations of the Tree, so that we have some interfaces like these:
public interface TreeNodeWithX extends TreeNode {
String getX();
void setX(String x);
}
public interface TreeNodeWithY extends TreeNode {
Boolean getY();
void setY(Boolean y);
}
So, I need that an object that is TreeNodeWithX (yes, an implementation of it) returns a TreeNodeWithX Object from its getParent method (same for the other methods from the TreeNode Interface).
Same behaviour from TreeNodeWithY, getParent() should return a TreeNodeWithY.
I have tried with some generics approaches, for instance this:
public interface TreeNode<T extends TreeNode> {
T getParent();
void setParent(T parent);
List<T> getChildren();
void setChildren(List<T> children);
}
However I always keep getting into trouble at some point in the implementation of the methods. My question is, am I going the right way with my generic interface or what am I doing wrong here?
The kind of recursive generic references are not really helping me out...