title: Type safety: Unchecked cast when extending generics
body: <p>I have the following method</p>
  
  <pre><code>private ResponseEntity&lt;? extends ResourceSupport&gt; createResponse(boolean isError) {
      if(isError){
          return new ResponseEntity&lt;ErrorResource&gt;((ErrorResource) new ErrorResource());
      }
      return new ResponseEntity&lt;ResourceSupport&gt;(new ResourceSupport());
  }
  </code></pre>
  
  <p>I call this method to get the error resource</p>
  
  <pre><code>ResponseEntity&lt;ErrorResource&gt; e = (ResponseEntity&lt;ErrorResource&gt;) createResponse(logoutResult, userId);
  System.out.println(e.getError());
  </code></pre>
  
  <p>When I do this I get this error:</p>
  
  <pre><code>"Type safety: Unchecked cast from ResponseEntity&lt;capture#1-of ? extends ResourceSupport&gt; to ResponseEntity&lt;ErrorResource&gt;"
  </code></pre>
  
  <p>Is there a way to cast the object without suppressing the error message?</p>