title: How to return a generic ArrayList<T>
body: <p>Sorry didnt know who to formulate the question best, but i will try to explain my Problem here, i have following class:</p>
  
  <pre><code>public class ReadOnlyTable&lt;T extends Model&gt; implements ReadOnly&lt;T&gt; {
  ...
  
  protected ReadOnlyTable() {
      initTable();
  }
  
  protected void reloadDataSource() {
      initTable();
  }
  
  ...
  
  @Override
  public ArrayList&lt;T&gt; findAll() {
      ArrayList&lt;T&gt; result = null;
  
      try {
          long startTime = System.currentTimeMillis();
          result = datasource.getAllEntries();
          //  // Logger.getLogger().write("FindAllQuery time was " + (System.currentTimeMillis() - startTime) + "ms");
      } catch (Exception e) {
          e.printStackTrace();
          return null;
      }
  
      return result;
  }
  </code></pre>
  
  <p>Lets say T is class "Galaxy", then i tried to loop over all elements in the returned Array, with following Code</p>
  
  <pre><code>    for (Galaxy gal : gDAO.findAll()) {
  
      }
  </code></pre>
  
  <p>Why does it give me an error that Galaxy is required, but Object is found?? What am i doing wrong, that i have return type ArrayList and not ArrayList&#60;T&#62;</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>gDAO is defined this way</p>
  
  <pre><code>private static GalaxyDAO gDAO = (GalaxyDAO) DAOFactory.get(GalaxyDAO.class);
  </code></pre>
  
  <p>And DAOFactory looks like this</p>
  
  <pre><code>    public static &lt;T&gt; T get(Class clazz) {
      if (!instanceList.containsKey(clazz)) {
          try {
  
              GenericDAO genericDAO = null;
              Constructor constructor;
              constructor = clazz.getDeclaredConstructor();
              constructor.setAccessible(true);
              genericDAO = (GenericDAO) constructor.newInstance();
              instanceList.put(clazz, genericDAO);
  
              return (T)genericDAO;
          } catch (NoSuchMethodException ex) {
              DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
          } catch (SecurityException ex) {
              DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
          } catch (InstantiationException ex) {
              DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
          } catch (IllegalAccessException ex) {
              DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
          } catch (InvocationTargetException ex) {
              DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
          }
      }else{
          return (T)instanceList.get(clazz);
      }
  
      return null;
  }
  </code></pre>
  
  <p>and finally</p>
  
  <pre><code>public class GalaxyDAO extends ReadWriteTable&lt;Galaxy&gt; implements GenericDAO {
  </code></pre>
  
  <p>and yeah .. ReadWriteTable extends ReadOnlyTable</p>
  
  <pre><code>public abstract class ReadWriteTable&lt;T extends Model&gt; extends ReadOnlyTable implements ReadWrite&lt;T&gt; {
  public ReadWriteTable() {              
      super();
  }    
  </code></pre>
  
  <p><strong>Appendix to acknowledged solution</strong></p>
  
  <p>I had a mistake in my inferfaces which prevented to give the Type to ReadOnlyTable see</p>
  
  <pre><code>public interface ReadWrite&lt;T&gt; extends ReadOnly {
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>public interface ReadWrite&lt;T&gt; extends ReadOnly&lt;T&gt; {
  </code></pre>
  
  <p>after fixing that i could also change following line</p>
  
  <pre><code>public abstract class ReadWriteTable&lt;T extends Model&gt; extends ReadOnlyTable&lt;T&gt; implements ReadWrite&lt;T&gt; {
  </code></pre>