title: Why won't this cast?
body: <p>So I have a class:</p>
  
  <pre><code>public static class AVLTreeNode &lt;E extends Comparable&lt;E&gt;&gt; extends BST.TreeNode&lt;E&gt; {
      protected int height;
  
      public AVLTreeNode(E e) {
          super(e);
      }
  }
  </code></pre>
  
  <p>That extends another class:</p>
  
  <pre><code>public static class TreeNode&lt;E extends Comparable&lt;E&gt;&gt; {
      protected E element;
      protected TreeNode&lt;E&gt; left;
      protected TreeNode&lt;E&gt; right;
  
      public TreeNode(E e) {
          element = e;
      }
  }
  </code></pre>
  
  <p>And I am creating an <code>ArrayList</code> of type <code>TreeNode</code>, and trying to cast it to <code>AVLTreeNode</code>:</p>
  
  <pre><code>public void balancePath(E e) {
      ArrayList&lt;TreeNode&lt;E&gt;&gt; path = path(e);
      for (int i = path.size() - 1; i &gt;= 0; i--) {
          AVLTreeNode&lt;E&gt; a = (AVLTreeNode&lt;E&gt;)(path.get(i));
      //continued code not important...
  </code></pre>
  
  <p>Note that my path method returns an <code>ArrayList</code> of type <code>TreeNode&lt;E&gt;</code>. But when I try to cast the node that I get at position i in the list to <code>AVLTreeNode&lt;E&gt;</code> (a subtype of <code>TreeNode</code>) I get a <code>ClassCastException</code>.</p>
  
  <p>What is the problem here?</p>
  
  <p><em>Edit</em> Here is the full stack trace</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: com.jeffsite.chapter27.BinarySearchTree$TreeNode cannot be cast to com.jeffsite.chapter29.AVLTree$AVLTreeNode
  at com.jeffsite.chapter29.AVLTree.balancePath(AVLTree.java:102)
  at com.jeffsite.chapter29.AVLTree.insert(AVLTree.java:19)
  at com.jeffsite.chapter29.TestAVLTree.main(TestAVLTree.java:10)
  </code></pre>