title: Java collections covariance problem
body: <p>Lets say we have a program which contains such classes:</p>
  
  <pre><code>public interface AbstractItem {
  }
  public SharpItem implements AbstractItem {
  }
  public BluntItem implements AbstractItem {
  }
  
  public interface AbstractToolbox {
      //well the problem starts here...
      public List&lt;AbstractItem&gt; getItems();
  }
  public ExpensiveToolbox implements AbstractToolbox {
      private List&lt;SharpItem&gt; items = new ArrayList()&lt;SharpItems&gt;;
      public List&lt;SharpItem&gt; getItems() { return this.items; }
  }
  public CheapTooblox implements AbstractToolbox {
      private List&lt;BluntItem&gt; items = new ArrayList()&lt;BluntItem&gt;;
      public List&lt;BluntItem&gt; getItems() { return this.items; }
  }
  </code></pre>
  
  <p>Easy, right? Well lets say we now want to make a method like this (in some random class):</p>
  
  <pre><code>public void doImportantStuff(AbstractToolbox toolbox) {
  //important stuff!
  //this obviously won't work
      List&lt;AbstractToolbox&gt; items = toolbox.getItems();
  //do some stuffwith all items
  }
  </code></pre>
  
  <p>Now the problem is that in Java collections with generics aren't covariant (hope that's the term I'm looking for) and I can't assign an <code>ArrayList&lt;ExpensiveToolbox&gt;</code> to a <code>List&lt;AbstractToolbox&gt;</code>. The only solution I can see here is to duplicate the code and do a version for each type, but that would obviously suck (what if we had more classes implementing AbstractToolbox with different lists?). Oh obviously the second solution would be to drop the generics and make a normal List, but is it a good practice?</p>
  
  <p>Are there any design pattern/practices to tackle such problems?</p>
  
  <p>@Edit: ok so I might not be precise enough. I want all the classes which extend AbstractToolbox to have a List of certain classes which extend AbstractItem and then I want a method that will take an AbstractToolbox as a parameter and do something on the items in its list (using the classes that would be defined in AbstractItem so all the items of every possible list would actually have them).</p>