title: Java generics: incompatible wildcard capture
body: <p>I have a repository interface parameterized with stored entities type. Among other methods in it, I have two of interest: the <code>create()</code> method which instantiates an entity, and the <code>save()</code> method that saves the entity:</p>
  
  <pre><code>public interface INamedEntityRepository&lt;T extends INamedEntity&gt; {
      T create(String name);
      void save(T namedEntity);
  }
  ...
  </code></pre>
  
  <p>Next follows a snippet of how this interface is used. I am getting a compilation error saying the type returned from the <code>create()</code> and that passed into <code>save()</code> are incompatible.</p>
  
  <pre><code>INamedEntityRepository&lt;? extends INamedEntity&gt; repo = getEntityRepository();
  
  INamedEntity toSave = repo.create("Named");
  ... // configure the entity more...
  repo.save(toSave);
        ^
  </code></pre>
  
  <p><code>The method save(capture#7-of ? extends INamedEntity) in the type INamedEntityRepository&lt;capture#7-of ? extends INamedEntity&gt; is not applicable for the arguments (INamedEntity)</code>. I can understand this, because really <code>INamedEntity</code> is not necessarily the expected type.</p>
  
  <p>But even doing this</p>
  
  <pre><code>repo.save(repo.create("Named")));
  </code></pre>
  
  <p>won't help:</p>
  
  <p><code>The method save(capture#7-of ? extends INamedEntity) in the type INamedEntityRepository&lt;capture#7-of ? extends INamedEntity&gt; is not applicable for the arguments (capture#8-of ? extends INamedEntity)</code>.</p>
  
  <p>What's the problem? How to correctly handle this situation?
  Thanks in advance.</p>