Results 1 to 1 of 1

Thread: clash of types

  1. #1
    t638403 is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default 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:
    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
    }
    Now I'd like to extend the Node class:
    Java 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:
    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();
    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:
    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
    Last edited by t638403; 11-03-2011 at 09:15 PM.

Similar Threads

  1. Variable types
    By Eleeist in forum New To Java
    Replies: 4
    Last Post: 07-27-2011, 05:04 PM
  2. Enumerated Types
    By muhsy in forum Advanced Java
    Replies: 2
    Last Post: 01-27-2011, 08:18 AM
  3. types of stacks
    By vendetta in forum New To Java
    Replies: 3
    Last Post: 02-06-2010, 03:58 AM
  4. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 10:29 PM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •