Results 1 to 1 of 1
Thread: clash of types
- 11-03-2011, 01:19 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
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:
Now I'd like to extend the Node class:Java 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 }
This class has inheritted the functions of Node so in my main loop I want to do the folowing:Java Code:public class SearchNode extends Node { private Location loc; public SearchNode(Location loc) { this.loc = loc; } }
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:Java Code:SearchNode sn1 = new SearchNode(new Location(10, 12)); SearchNode sn2 = new SearchNode(new Location(45,88)); sn2.setParent(sn1); SeachNode parent = sn2.getParent();
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 :-)Java Code:public class Node<T> { T parent; public void setParent(T parent) { this.parent = parent; } public T getParent() { return this.parent; } }
Can anybody help me?
Thanks in advance,
ErikLast edited by t638403; 11-03-2011 at 09:15 PM.
Similar Threads
-
Variable types
By Eleeist in forum New To JavaReplies: 4Last Post: 07-27-2011, 05:04 PM -
Enumerated Types
By muhsy in forum Advanced JavaReplies: 2Last Post: 01-27-2011, 08:18 AM -
types of stacks
By vendetta in forum New To JavaReplies: 3Last Post: 02-06-2010, 03:58 AM -
generic types
By jon80 in forum New To JavaReplies: 6Last Post: 06-12-2009, 10:29 PM -
name clash: equals(E) in and equals(java.lang.Object)
By AdRock in forum New To JavaReplies: 0Last Post: 01-25-2008, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks