title: Java Generics - Why returning super class instance gives compile error
body: <p>Hi the following code gives compile error but I do not understand why.</p>
  <pre class="lang-java prettyprint-override"><code>class SomeClass &lt;T extends BaseClass&gt; {
      T method(T x) {
          return x;                   // OK
      }
      
      T method() {
          return new BaseClass();    // compile error
      }
  }
  </code></pre>
  <p>The error says requires <code>T</code> but provided <code>BaseClass</code>.<br>
  And similarly this one:</p>
  <pre class="lang-java prettyprint-override"><code>class SomeClass {
      &lt;T extends BaseClass&gt; T method(T x) {
          return x;                          // OK
      }
      
      &lt;T extends BaseClass&gt; T method() {
          return new BaseClass();           // compile error
      }
  }
  </code></pre>
  <p>I know that it can be resolved like this:</p>
  <pre class="lang-java prettyprint-override"><code>class SomeClass &lt;T extends BaseClass&gt; {
      T method(T x) {
          return x;                       // OK
      }
      
      @SuppressWarnings( &quot;unchecked&quot; )
      T method() {
          return (T) new BaseClass();    // OK
      }
  }
  </code></pre>
  <p>But may I have some explanation on why this is not allow?</p>