-
clash of types
Hi everyone,
I have a rather basic question here, though I can't figure it out myself.
I want to create a special Node class for use in a search tree. This node I like to call SearchNode which should extend the simpler Node class. The simple Node class looks like this:
Code:
public class Node {
private Node parent;
private LinkedList<Node> children;
public void setParent(Node parent) {
this.parent = parent;
}
public void getParent() {
return this.parent;
}
// And some more functions
}
Now I'd like to extend the Node class:
Code:
public class SearchNode extends Node {
private Location loc;
public SearchNode(Location loc) {
this.loc = loc;
}
}
This class has inheritted the functions of Node so in my main loop I want to do the folowing:
Code:
SearchNode sn1 = new SearchNode(new Location(10, 12));
SearchNode sn2 = new SearchNode(new Location(45,88));
sn2.setParent(sn1);
SeachNode parent = sn2.getParent();
For obvious reasons it starts complaining that getParent() is going to return a Node, and not a SearchNode (I could cast, but I don't want to). So I want my Node to be more generic. So I figure, let's get into generics :-). So I did something like this:
Code:
public class Node<T> {
T parent;
public void setParent(T parent) {
this.parent = parent;
}
public T getParent() {
return this.parent;
}
}
But as a matter of fact, it keeps complaining. So I figure, I do not have enough knowledge, and I need to ask someone who has :-)
Can anybody help me?
Thanks in advance,
Erik