title: Can Java Generics replace multiple similar classes?
body: <h3>Problem</h3>
  
  <p>I'm trying to use Java generics to replace classes with similar methods. All of the examples that I've found cover simple examples, but I'm not sure if Java Generics are intended to be used this way.</p>
  
  <p>I have 2 parent classes, and 2 child classes that have almost identical methods. Both parent classes derive from different classes as well. Ultimately, I want to be able to use one block of code to create and manipulate one of the parent classes, then its child class without a lot of switch statements or other flow control with duplicate code.</p>
  
  <p>This is what I had in mind, even though I haven't beeen able to get it to work this way yet, whether it be syntax, or just not a feature of Generics.</p>
  
  <h3>Parent Classes</h3>
  
  <pre><code>public class FooParent
  {
      private FooChild fooChild;
      public FooChild getChild()
      {
          return fooChild;
      }
  }
  
  public class BarParent
  {
      private BarChild barChild;
      public BarChild getChild()
      {
          return barChild;
      }
  }
  </code></pre>
  
  <h3>Child Classes</h3>
  
  <pre><code>public class FooChild
  {
      public void print()
      {
          System.out.println("I'm a foo child");
      }
  }
  
  public class BarChild
  {
      public void print()
      {
          System.out.println("I'm a bar child");
      }
  }
  </code></pre>
  
  <h3>Generic Classes</h3>
  
  <pre><code>public class GenericParent&lt;T&gt;
  {
      private T self;
      public GenericParent(T self)
      {
          this.self = self;
      }
  
      public GenericChild getChild()
      {
          return new GenericChild(self.getChild());
      }
  }
  
  public class GenericChild&lt;T&gt;
  {
      private T self;
      public GenericChild(T self)
      {
          this.self = self;
      }
  
      public void print()
      {
          self.print();
      }
  }
  </code></pre>
  
  <h3>How I <em>want</em> to use them</h3>
  
  <pre><code>public static void main(String args[])
  {
      GenericParent parent;
  
      // Only the initialization of the parent variable needs specialized code
      switch(args[0])
      {
          case "foo":
              parent = new GenericParent(new FooParent());
          break;
  
          case "bar":
              parent = new GenericParent(new BarParent());
          break;
      }
  
      // From here on out, it's all generic
      parent.getChild().print();
  }
  </code></pre>
  
  <h3>Usage and desired output</h3>
  
  <pre><code>java genericExample foo
  &gt; I'm a foo child
  
  java genericExample bar
  &gt; I'm a bar child
  </code></pre>
  
  <h3>Final Questions</h3>
  
  <p>Maybe "child" and "parent" are misnomers, because I know they're not actually inherited, but the bottom line is, the one class returns its "child" with certain methods. So this is a lot of code for a problem that may not actually be solvable this way, but hopefully you can answer me this:</p>
  
  <ol>
  <li>Is this something that Java Generics can accomplish?</li>
  <li>If not, is there a solution to this problem in Java?</li>
  </ol>
  
  <p>Thanks!</p>
  
  <h3>Edit</h3>
  
  <p>My "Foo" and "Bar" classes are uneditable by me. My ultimate question is: can I store one instance of either class in a single variable without using a common parent class?</p>