title: Automatic cast to subclass in getter/setter
body: <p>I have a rather weird question. Consider the following two classes:</p>
  
  <pre><code>public class Node&lt;T&gt; {
      private Node&lt;T&gt; parent;
      private Node&lt;T&gt; left;
      private Node&lt;T&gt; right;
      private T data;
  
      public Node(Node&lt;T&gt; parent, T data) {
          this.parent = parent;
          this.data = data;
      }
  
      public Node&lt;T&gt; getParent() {
          return parent;
      }
  
      public void setParent(Node&lt;T&gt; parent) {
          this.parent = parent;
      }
  
      public Node&lt;T&gt; getLeft() {
          return left;
      }
  
      public void setLeft(Node&lt;T&gt; left) {
          this.left = left;
      }
  
      public Node&lt;T&gt; getRight() {
          return right;
      }
  
      public void setRight(Node&lt;T&gt; right) {
          this.right = right;
      }
  
      public T getData() {
          return data;
      }
  
      public void setData(T data) {
          this.data = data;
      }
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public class RedBlackNode&lt;T&gt; extends Node&lt;T&gt; {
  
      private Color color;
  
      public RedBlackNode(Node&lt;T&gt; parent, T data) {
          super(parent, data);
          this.color = Color.Black;
      }
  
      public RedBlackNode(Node&lt;T&gt; parent, T data, Color color) {
          super(parent, data);
          this.color = color;
      }
  
      public Color getColor() {
          return color;
      }
  
      public void setColor(Color color) {
          this.color = color;
      }
  
  }
  </code></pre>
  
  <p>My question is .. is there any way in which I could write:</p>
  
  <pre><code>RedBlackNode&lt;Whatever&gt; x = y.getLeft();
  </code></pre>
  
  <p>where y is a <code>RedBlackNode</code> without having to specifically cast using:</p>
  
  <pre><code>RedBlackNode&lt;Whatever&gt; x = (RedBlackNode&lt;Whatever&gt;) y.getLeft();
  </code></pre>
  
  <p>and also, without having to override the getters/setters in the RedBlackNode class? </p>
  
  <p>I mean, I just want to somehow make the RedBlackNode be "aware" that it's a RedBlackNode and know that all its children/parent are actually RedBlackNodes as well. I am not sure but maybe somehow add a new abstraction layer on top of the Node class somehow maybe? </p>
  
  <p>Ty!</p>
  
  <p>Later edit:</p>
  
  <p>Thank you for all your prompt answers, this is what I have so far:</p>
  
  <pre><code>public abstract class AbstractNode&lt;T, U extends AbstractNode&lt;T, U&gt;&gt; {
      private AbstractNode&lt;T, U&gt; parent;
      private AbstractNode&lt;T, U&gt; left;
      private AbstractNode&lt;T, U&gt; right;
      private T data;
  
      public AbstractNode(AbstractNode&lt;T, U&gt; parent, T data) {
          this.parent = parent;
          this.data = data;
      }
  
      public AbstractNode&lt;T, U&gt; getParent() {
          return parent;
      }
  
      public void setParent(AbstractNode&lt;T, U&gt; parent) {
          this.parent = parent;
      }
  
      public AbstractNode&lt;T, U&gt; getLeft() {
          return left;
      }
  
      public void setLeft(AbstractNode&lt;T, U&gt; left) {
          this.left = left;
      }
  
      public AbstractNode&lt;T, U&gt; getRight() {
          return right;
      }
  
      public void setRight(AbstractNode&lt;T, U&gt; right) {
          this.right = right;
      }
  
      public T getData() {
          return data;
      }
  
      public void setData(T data) {
          this.data = data;
      }
  
  }
  
  public class RedBlackNode&lt;T extends Element&gt; extends AbstractNode&lt;Element, RedBlackNode&lt;Element&gt;&gt; {
  
      private Color color;
  
      public RedBlackNode(RedBlackNode&lt;Element&gt; parent, T data) {
          super(parent, data);
          this.color = Color.Black;
      }
  
      public RedBlackNode(RedBlackNode&lt;Element&gt; parent, T data, Color color) {
          super(parent, data);
          this.color = color;
      }
  
      public Color getColor() {
          return color;
      }
  
      public void setColor(Color color) {
          this.color = color;
      }
  
  }
  
  
  public class Node&lt;T&gt; extends AbstractNode&lt;T, Node&lt;T&gt;&gt; {
  
      public Node(AbstractNode&lt;T, Node&lt;T&gt;&gt; parent, T data) {
          super(parent, data);
      }
  
  }
  </code></pre>
  
  <p>However, I need to create 2 types of trees. A simple Binary Search Tree which has as nodes only instances of class Node and, as expected, a Red Black Tree which has only instances of RedBlackNode as nodes.</p>
  
  <p>I made an abstract class for these trees (with many more methods, this is the trimmed version) but I fear that I am missing something as I seem to have this <code>Node&lt;Element&gt;</code> as the generic type in the class definition.</p>
  
  <pre><code>public abstract class Tree&lt;T extends AbstractNode&lt;Element, Node&lt;Element&gt;&gt;&gt; {
  
      protected T root;
  
      public T getRoot() {
          return this.root;
      }
  
      public abstract T search(T n, int key);
  
      public abstract T insert(Element e, T cRoot);
  }
  </code></pre>
  
  <p>What am I missing here? How to best model this?</p>
  
  <hr>
  
  <p>Later edit 2:</p>
  
  <p>I currently have:</p>
  
  <pre><code>public abstract class AbstractNode&lt;E extends Element, U extends AbstractNode&lt;E, U&gt;&gt; {
      private U parent;
      private U left;
      private U right;
      private E data;
  
      public AbstractNode(U parent, E data) {
          this.parent = parent;
          this.data = data;
      }
  
      public U getParent() {
          return parent;
      }
  
      public void setParent(U parent) {
          this.parent = parent;
      }
  
      public U getLeft() {
          return left;
      }
  
      public void setLeft(U left) {
          this.left = left;
      }
  
      public U getRight() {
          return right;
      }
  
      public void setRight(U right) {
          this.right = right;
      }
  
      public E getData() {
          return data;
      }
  
      public void setData(E data) {
          this.data = data;
      }
  
  }
  
  public class Node&lt;E extends Element&gt; extends AbstractNode&lt;E, Node&lt;E&gt;&gt; {
  
      public Node(Node&lt;E&gt; parent, E data) {
          super(parent, data);
      }
  
  }
  
  public class RedBlackNode&lt;E extends Element&gt; extends AbstractNode&lt;E, RedBlackNode&lt;E&gt;&gt; {
  
      private Color color;
  
      public RedBlackNode(RedBlackNode&lt;E&gt; parent, E data) {
          super(parent, data);
          this.color = Color.Black;
      }
  
      public RedBlackNode(RedBlackNode&lt;E&gt; parent, E data, Color color) {
          super(parent, data);
          this.color = color;
      }
  
      public Color getColor() {
          return color;
      }
  
      public void setColor(Color color) {
          this.color = color;
      }
  
  }
  </code></pre>
  
  <p>The Tree class:</p>
  
  <pre><code>public abstract class Tree&lt;E extends Element, T extends AbstractNode&lt;E, T&gt;&gt; {
  
      protected T root;
  
      public T getRoot() {
          return this.root;
      }
  
      public abstract T search(T n, int key);
  }
  </code></pre>
  
  <p>AND The <code>BinarySearchTreeClass</code></p>
  
  <pre><code>public class BinarySearchTree&lt;Element&gt; extends Tree&lt;Element, Node&lt;Element&gt;&gt; {
  
      @Override
      public Node&lt;Element&gt; search(Node&lt;Element&gt; n, int key) {
          if (n == null || n.getData().getIntegerData() == key) {
              return n;
          }
          return key &lt; n.getData().getIntegerData() ? search(  n.getLeft(), key) : search( n.getRight(), key);
      }
  
  }
  </code></pre>
  
  <p>Why in the world am I getting <code>Bound mismatch: The type Element is not a valid substitute for the bounded parameter &lt;E extends Element&gt; of the type Node&lt;E&gt;</code>. It's <code>E extends Element</code>. Doesn't that mean it can be of type <code>Element</code> as well?
  Nevertheless, I tried using something like a separate class which extended <code>Element</code> but to no avail..</p>
  
  <p>Also, I am getting a <code>The type parameter Element is hiding the type Element</code> and I have no idea why..</p>
  
  <p>I created a <a href="https://gist.github.com/Alex-Cosma/3378f475adb78a42c2c0" rel="nofollow">GIST</a> for this.</p>