The title may be a bit misleading but I was not sure how to word this.
I have 2 classes, BinarySearchTree and HashTreeSet.
I am currently using HashTreeSet to implement a variant of BinarySearchTree using different sorts, compares and such.
Info:
I have one problem and one problem only so far: HashTreeSet complains about my find(T target) function, saying the return-type Node<T>
is incompatible with BinarySearchTree<T>.find(T)
.
BinarySearchTree implements the method find(T target).
I wanted to change some of the find-code in the HashTreeSet class, so I copied the function over, but now it complains about some return-statement.
I'll post parts of the code to avoid clutter, let me know if you want the full versions:
HashTreeSet
public class HashTreeSet<T extends Comparable<T>> extends BinarySearchTree<T> implements SimpleSet<T> {
/*
* Other functions
*/
The error is highlighted here, see error message below
|
|
V
public Node<T> find(T target) {
Node<T> current = findAgain(target, root);
if( current == null )
return null;
return current;
}
private Node<T> findAgain(T target, Node<T> next){
// Stuff
}
class Node<T extends Comparable<T>> {
// Stuff
}
}
BinarySearchTree
public class BinarySearchTree<T extends Comparable<T>> implements SimpleSet<T> {
/*
* Other functions
*/
public Node<T> find(T target) {
Node<T> current = findAgain(target, root);
if( current == null )
return null;
return current;
}
private Node<T> findAgain(T target, Node<T> next){
// Stuff
}
class Node<T extends Comparable<T>> {
// Stuff
}
}
Error
The return type is incompatible with BinarySearchTree.find(T)
When I attempt to QuickFix it, it changes the return-statement from Node<T>
to treestuff.Node<T>
but it still remain red and uncompilable. treestuff
is the package in which both of these files are contained.
You've got two different Node<T>
classes - one as an inner class of BinarySearchTree
and one as an inner class of HashTreeSet
. They're completely different classes, so your override is trying to return an unrelated type to the return type of the original method... it's no wonder the compiler it complaining.
Do you need two different types? I would hope not - but if you really do, make HashTreeSet.Node
extend BinarySearchTree.Node
. I'd personally recommend pulling out the Node
type as a top-level class, too - that will make the code simpler in other respects.
Additionally, it's not really clear that HashTreeSet
should extend BinarySearchTree
at all - typically you have a binary tree or you use hashing... not both. Are you sure that your inheritance here is appropriate?
See more on this question at Stackoverflow