title: How to Create a Linked List with Generics
body: <p>I'm trying to write my own Linked List class in Java using generics. The linked list should work with two different Node classes I have created. I want to be able to call methods of the node classes that is used in my Linked List, but obviously it doesn't work the way I am trying to do it. Each node class will have all of the same methods (and method names), but the methods for each will do different things. With this I have a few questions:</p>
  
  <p>1) What is the "work around" (if any) to calling methods on generic type variables. In my case, I am trying to call methods of my node class from my linked list class. </p>
  
  <p>2) How do I make sure the only two data types my Linked List can have are the two node classes I define?</p>
  
  <p>3) Am I thinking about this completely wrong? i.e. Should I be doing this an entirely different way?</p>
  
  <p>Here is a simplified version of the code.</p>
  
  <pre><code>public class Node1 {
  
      public int stuff1;
      public Node1 nextNode;
  
      Node1() {
          this.nextNode = null;
      }
  
      public void doStuff(){
          System.out.println("Node 1 did stuff");
      }
  
  }
  
  public class Node2 {
  
      public int stuff2;
      public Node2 nextNode;
  
      Node2() {
          this.nextNode = null;
      }
  
      public void doStuff(){
          System.out.println("Node 2 did other stuff");
      }
  
  }
  
  public class LinkedList&lt;T&gt;{
  
      public T first; // do I need to construct/instantiate this too?
  
      public void doStuff() {
          first.doStuff;
      }
  
      public static void main(String[] args){
          LinkedList&lt;Node1&gt; node1 = new LinkedList&lt;Node1&gt;;
          LinkedList&lt;Node2&gt; node2 = new LinkedList&lt;Node2&gt;;
          node1.doStuff();
          node2.doStuff();
      }
  
  }
  </code></pre>
  
  <p>Thanks!</p>