title: extracting all instances of subclass from arraylist
body: <p>I have <code>ArrayList&lt;Unit&gt; units</code>. I want to write a function that would return all objects of specified subclass, which is used as parameter. However I can't get it to work. Here is what I have:</p>
  
  <pre><code>public static ArrayList&lt;? extends Unit&gt; getTheseUnits(Class&lt;? extends Unit&gt; specific_unit) {
      ArrayList&lt;specific_unit&gt; res = new ArrayList&lt;&gt;();  //'specific_unit' is in red here. Adding '.class' or '.getClass()' after it does not resolve anything
      for (Unit u : units){
          if (u instanceof specific_unit){
              res.add(u);
          }
      }
      return res;
  }
  </code></pre>