Search code examples
javainterfacecomparable

How to implement: interface MySortedCollection<T extends Comparable<T>>


I have the following interface:

interface MySortedCollection<T extends Comparable<T>> {
    boolean isElement(T t);
    void insert(T t);
    void printSorted();
}

I tried to use the AVLTree to implement the interface:

public class AVLTree<T> implements MySortedCollection{

  private AVLNode<T> tree=null;

  public AVLTree (){
  } 

  public boolean isElement(T t){

  }


  public void insert(T t){
    if(tree==null){
      tree= new AVLNode<T>(t);
    }
  }

  public void printSorted(){}

}

But I got the error:

error: AVLTree is not abstract and does not override abstract
method insert(Comparable) in MySortedCollection 
public class AVLTree<T> implements MySortedCollection{

What's wrong?


Solution

  • It should be

    public class AVLTree<T extends Comparable<T>> implements MySortedCollection<T> {
    }
    

    Make sure that AVLNode class has a similar signature

    public class AVLNode<T extends Comparable<T>> {
    }