Getter method for Generic type in BinarySearchTree

Hi, I had to write an implementation of a BinarySearchTree for an assignment.

I have the two classes, one 'BinarySearchTree' which basicly just points to the root, and the node that does most of the work:

publicclass BinaryNode<Kextends Comparable><K>,V>

{

public K _key;

public V _val;

private BinaryNode _left;

private BinaryNode _right;

....

public V getVal()//getter

{

return _val;

}

}

In my BinarySearchTree class I use this getter method like so:

V nodeValue;

nodeValue = (V) oldNode.getVal();

The thing I don't understand is why I have to cast the value returned? If I don't it won't compile!

It is working fine as is, but my lecturer is always saying to avoid casts.

Thanks,

Austin.

[1276 byte] By [austinmartona] at [2007-11-27 4:38:46]
# 1
Your BinaryNode class looks OK.Could you post a small piece of code I can compile and run and results in the compile error you described? (Don't post the entire thing!)Thanks.
prometheuzza at 2007-7-12 9:49:16 > top of Java-index,Java Essentials,New To Java...
# 2
It seems that you use BinaryNode oldNode = new BinaryNode(); declaration.Try the following one: BinaryNode<K, V> oldNode = new BinaryNode<K, V>();
Ilya_Markova at 2007-7-12 9:49:16 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks, that was it.
austinmartona at 2007-7-12 9:49:16 > top of Java-index,Java Essentials,New To Java...