title: Java generics with upper bound
body: <p>Say I have following sample application, I'm using java 8.</p>
  
  <p>I have following class that has a child class:</p>
  
  <pre><code>public class GrantedAuthority { }
  public class GrantedAuthoritySubClass extends GrantedAuthority { }
  </code></pre>
  
  <p>Following class is where the tricky part happens:</p>
  
  <pre><code>public class UserDetails {
  
      Collection&lt;? extends GrantedAuthority&gt; authorities;
  
      public Collection&lt;? extends GrantedAuthority&gt; getAuthorities(){
          return authorities;
      }
  }
  </code></pre>
  
  <p>My main method:</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) {
  
          UserDetails u = new UserDetails();
          List&lt;GrantedAuthority&gt; list = new ArrayList&lt;GrantedAuthority&gt;();
          list.add(new GrantedAuthoritySubClass());
          u.getAuthorities().addAll(list);
      }
  }
  </code></pre>
  
  <p>When I run this I get the following error:</p>
  
  <blockquote>
    <p>Error:(12, 35) java: incompatible types: <code>java.util.List&lt;GrantedAuthority&gt;</code> cannot be converted to <code>java.util.Collection&lt;? extends capture#1 of ? extends GrantedAuthority&gt;</code></p>
  </blockquote>
  
  <p>My problem is, how I can pass a collection of <code>GrantedAuthoritySubClass</code> Objects to the collection returned by <code>u.getAuthorities()</code>. I can't figure it out and there are so many compilation errors attached to various ways I've tried.</p>