Search code examples
javaextendscomparabletreenodekeyvaluepair

Java - extension reversal


If my program is using a Class TreeNode that extends a ComparableKeyValuePair

Where:

Root can be a TreeNode and TreeNode is a ComparableKeyValuePair Therefor, Root is a ComparableKeyValuePair,

How can I declare something like this

TreeNode node;
ComparableKeyValuePair tempNode;

node = tempNode;

Solution

  • If tempNode is not a TreeNode, then you cannot do this. If it is, you can write node = (TreeNode)tempNode. If the cast fails, you'll get a ClassCastException.