title: Android - using Java generics to return ArrayList of derived class
body: <p>I have an android app with a Business Object (let's call it SuperClass) that has several derived classes (let's call them DerviedClassA, DerivedClassB etc). Each object has a constructor which takes a Cursor object with multiple rows and assigns appropriate properties based on the <em>current row in the cursor</em></p>
  
  <pre><code>public Class DerivedClassA
  {
      // properties
      private int X;
      // etc
  
      public DerivedClassA(Cursor data)
      {
           X = data.getInt(data.getColumIndex(ColumnX));
           // etc
      }
  }
  </code></pre>
  
  <p>I then have BO methods which return an ArrayList of the derived class, like below:</p>
  
  <pre><code>public static ArrayList&lt;DerivedClassA&gt; getDerivedClassAList(Cursor data)
  {
      ArrayList&lt;DerivedClassA&gt; list = new ArrayList&lt;DerivedClassA&gt;();
  
      for(int i=0;i&lt;data.getCount();i++)
      {
          data.moveToPosition(i);
          DerivedClassA wev = new DerivedClassA(data);
          list.add(wev);
      }
  
      data.close();
      return list;
  }
  </code></pre>
  
  <p>And so on for DerivedClassB, DerivedClassC etc. To avoid this duplication, I want to be able to have a single method which somehow takes in the type of the derived class, invokes the appropriate constructor of that derived class, and returns an ArrayList of the appropriate derived class. Something like:</p>
  
  <pre><code>public static ArrayList&lt;T extends SuperClass&gt; getDerivedClassList(Cursor data)
  {
      ArrayList&lt;T&gt; list = new ArrayList&lt;T&gt;();
  
      for(int i=0;i&lt;data.getCount();i++)
      {
          data.moveToPosition(i);
          T wev = new T(data);
          list.add(wev);
      }
  
      data.close();
      return list;
  }
  </code></pre>
  
  <p>However, I'm not sure if it's actually possible, or how to go about it. Any pointers would be appreciated.</p>