title: How to let the return value remain generic (without casting) when a method of a class with a bounded type parameter returns a value of its own type?
body: <p>I have a class <code>A</code> which contains a field of type <code>A</code> (similar to the way a <code>Node</code> in an implementation of a linked list has a <code>Node nextNode</code>) like so:</p>
  
  <pre><code>public class A {
  
      private A otherA;
  
      public A getOtherA() {
          return otherA;
      }
  }
  </code></pre>
  
  <p>Then I have a class <code>B</code> with a bounded type parameter of <code>&lt;T extends A&gt;</code> which attempts to assign the return value from <code>getOtherA()</code> to a variable of type <code>T</code>:</p>
  
  <pre><code>public class B&lt;T extends A&gt; {
  
      public B(T t1) {
          T t2 = t1.getOtherA();
      }
  }
  </code></pre>
  
  <hr>
  
  <p>The above code produces an error at the line</p>
  
  <pre><code>T t2 = t1.getOtherA();
  </code></pre>
  
  <p>and reads as</p>
  
  <pre><code>Incompatible types.  Required T, Found A.
  </code></pre>
  
  <p>My question is <strong>how (if possible) do I modify the code so that I can treat the return value of <code>getOtherA()</code> as type <code>T</code></strong>.  After all, <code>t1</code> is of type <code>T</code>, <code>T</code> extends <code>A</code> and <code>getOtherA()</code> just returns an object of the same type as the object it belongs to. So why does <code>T#getOtherA()</code> not have a return type of <code>T</code>?</p>
  
  <p>It seems to me that my compiler should be satisfied that <code>t1.getOtherA()</code> always returns an object of type <code>T</code>, but I suspect that I have a misunderstanding of generics, in which case I'll bite the bullet and just cast each one to my desired type.</p>