title: Returning Subclass of generic superclass in a superclass method
body: <p>I am new to Java coming from a PHP background so sorry if this maybe obvious. I'm trying to implement a binary tree class and I've created an ADT like so:</p>
  
  <pre><code>public abstract class BTree&lt;T&gt; {
      private T value;
      private BTree&lt;T&gt; leftChild;
      private BTree&lt;T&gt; rightChild;
      private BTree&lt;T&gt; parent;
      public BTree&lt;T&gt; getLeftChild() { return this.leftChild; }
      ....
  } 
  </code></pre>
  
  <p>Then I have another class that extends this like so:</p>
  
  <pre><code>public class BIntTree extends BTree&lt;Integer&gt; {
  }
  </code></pre>
  
  <p>However I want to be able to within BIntTree to have a method where I can call <code>this.getLeftChild();</code> and get an instance of BIntTree back rather than a instance of <code>BTree&lt;Integer&gt;</code></p>
  
  <p>Is this possible with some way of defining the generic class / method or do I have to explicitly type cast it after I used this.getLeftChild() or even override the superclass method? </p>
  
  <p>My current solution is to explicitly typecast it in the BIntTree method with <code>BIntTree b=(BIntTree) this.getLeftChild();</code> which seems untidy to me.</p>
  
  <p>Also I'm not so sure what would happen if I had that type casting defined and getLeftChild() returned null, would an exception be thrown? If so how do I cure this given that null is also a valid value if exist?</p>