Reputation: 1
this is my first post here, but I need help trying to figure out why my child class will not let me override the parent method.
I was tasked to create a generic Binary Search Tree with some functionality. The next task was to create a generic AVL Tree and I decided to extend it from my custom Binary Search Tree to reuse code and simply add the rotation required to make it functional.
However when trying to overwrite the method, I keep getting a name clash error. And if I get rid of my Comparable interface extension in my AVLTree class and make my insert method generic, I get an argument mismatch error saying T cannot be converted to Comparable.
This is where I am stuck at, if anyone can offer any type of input it would be much appreciated.
public class BinaryTree <T extends Comparable<T>>{
Node Root;
public BinaryTree(){
this.Root = null;
}
public boolean isEmpty(){
return this.Root == null;
}
public void insert(T data){
this.insert(data, this.Root);
}
protected void insert(T data, Node<T> n){
if(this.Root == null){
this.Root = new Node(data);
}
if(data.equals(n.getData())){
n.occurances++;
}
else if(data.compareTo(n.data) < 0){
if(n.left != null)
insert(data, n.left);
else
n.left = new Node(data);
}
else if(data.compareTo(n.data) > 0){
if(n.right != null)
insert(data, n.right);
else
n.right = new Node(data);
}
}
First attempt:
public class AVLTree <T extends Comparable<T>> extends BinaryTree{
private static final int ALLOWED_IMBALANCE = 1;
public void insert(T data){
this.insert(data, this.Root);
}
Second attempt:
public class AVLTree extends BinaryTree{
private static final int ALLOWED_IMBALANCE = 1;
public <T>void insert(T data){
this.insert(data, this.Root);
}
error for overwriting insert method
Upvotes: 0
Views: 85
Reputation: 11531
Try passing the type parameter of BinaryTree
:
// vvv
public class AVLTree <T extends Comparable<T>> extends BinaryTree<T> {
private static final int ALLOWED_IMBALANCE = 1;
@Override
public void insert(T data){
this.insert(data, this.Root);
}
}
Upvotes: 1