title: Convert ArrayList<subclass> to ArrayList<superclass>
body: <p>I have three classes: Blob, Food and Predator, all of which are subclasses of the abstract class Organism.</p>
  
  <p>I also have an ArrayList for each class: blobs, foods and predators</p>
  
  <p>I'm writing a function that will choose a 'target' for an organism depending on what type it is. The organism is passed as a parameter and then an appropriate target is chosen - if a Blob is passed then it should target a Food, if a Predator is passed then it should target a Blob, and if a Food is passed then it should target a Predator. Right now I have:</p>
  
  <pre><code>ArrayList&lt;Food&gt; foods = new ArrayList&lt;Food&gt;();
  ArrayList&lt;Blob&gt; blobs = new ArrayList&lt;Blob&gt;();
  ArrayList&lt;Predator&gt; predators = new ArrayList&lt;Predator&gt;();
  
  public Organism pickTarget(Organism o){
      ArrayList&lt;Organism&gt; search = new ArrayList&lt;Organism&gt;();
      if(o.getClass() == Blob.class){
          search = foods;
      }
      else if(o.getClass() == Predator.class){
          search = blobs;
      }
      else if(o.getClass() == Food.class){
          search = predators;
      }
  
      Organism target = search.get(0);
      for(Organism organism:search){
          //do stuff to pick a target from assigned search space
      }
  
      return target;
  }
  </code></pre>
  
  <p>So the idea here is to intialise a generic Organisms ArrayList, then assign one of the existing ArrayLists to it depending on what type the parameter is. That ArrayList will then be searched for the most suitable target which is returned.</p>
  
  <p>But I'm getting a type mismatch error when I try to assign an existing ArrayList to the search variable. 'Cannot convert from <code>ArrayList&lt;Food&gt;</code> to <code>ArrayList&lt;Organism&gt;</code> ', for example.</p>
  
  <p>Does anybody know of a solution to a problem like this? Converting from a subclass to a superclass isn't usually an issue so I'm hoping that there is a solution for ArrayLists.</p>