title: Java Collections: Pass collection of children as collection of parents
body: <p>Say I have an interface and some classes:</p>
  
  <pre><code>public interface IPanel&lt;ComponentType extends Component&gt; {
     public void addComponents(Set&lt;ComponentType&gt; components);
     public ComponentType create();
  }
  
  public class Button extends Component { }
  
  public class LocalizedButton extends Button { }
  
  public class ButtonsPanel implements IPanel&lt;Button&gt; {
      public void addComponents(Set&lt;Button&gt; components) { ... /* uses create() */ ; }
      public Button create() { return new Button(); }
  }
  
  public class LocalizedButtonsPanel extends ButtonsPanel {
      public Button create() { return new LocalizedButton(); }
  }
  </code></pre>
  
  <p>Then I have a set of LocalizedButtons and when I call</p>
  
  <pre><code>final LocalizedButtonsPanel localizedButtonsPanel = new LocalizedButtonsPanel();
  final Set&lt;LocalizedButton&gt; localizedButtonsSet = new LinkedHashSet&lt;LocalizedButton&gt;();
  localizedButtonsPanel.addComponents(localizedButtonsSet);
  </code></pre>
  
  <p>I get that this method is not applicable for this arguments.
  If I try to overload this method as <code>addComponents(Set&lt;LocalizedButton&gt; buttons)</code> in <code>LocalizedButtonsPanel</code>, I get type erasure, of course.</p>
  
  <p>May be some pattern is missed or the trick exists to deal with this architecture to implement correct adding the set of LocalizedButtons?</p>
  
  <hr>
  
  <p>I've got the answer and I want to make my example more concrete - I have some validators in my implementation, so I need the collection type to be also stored as generic, that is simplified code I've got using the answer:</p>
  
  <pre><code>public interface IPanel&lt;ComponentType extends Component, CollectionType extends Collection&lt;? extends Component&gt;&gt; extends Validated&lt;CollectionType&gt; {
     public void addComponents(CollectionType components);
     public ComponentType create();
  }
  
  public class Button extends Component { }
  
  public class LocalizedButton extends Button { }
  
  public class ButtonsPanel implements IPanel&lt;Button, Set&lt;? extends Button&gt;&gt; {
      public void addComponents(Set&lt;? extends Button&gt; components) { ... /* uses create() */ ; }
      public Button create() { return new Button(); }
  }
  
  public class LocalizedButtonsPanel extends ButtonsPanel {
      public Button create() { return new LocalizedButton(); }
  }
  </code></pre>
  
  <p>And in this case, it works</p>