Instructor Comparable not being inherited from User

Hi,

I am running through the Java Advanced Topics - Generic Classes and inheritance.

I am using three classes:

  • GenericList<T Extends Comparable>
  • User (Implements Comparable)
  • Instructor (extends user)

I can create an instance of GenericList using User
I get an error when I create a GenericList of Instructor

This doesn’t seem to be a problem in the videos. Below is the code for the classes and a screenshot of the error…

Here is the code:

GenericList:

public class GenericList<T extends Comparable<T>> {
    private T[] items;
    private int count;

    public GenericList() {
        items =(T[])new Comparable[10];
        count = 0;
    }

    public void addItem(T item){
        items[count++] = item;
    }

    public T getItem(int pos){
        return items[pos - 1];
    }
}

User

public class User implements Comparable<User> {
    private String email;

    public User(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public int compareTo(User o) {
        return (email.compareTo(o.email));

    }
}

Instructor

public class Instructor extends User {
    public Instructor(String email){
        super(email);
    }
}

And here is the error:

I needed to change the GenericList code to:

GenericList<T extends Comparable<? super T>>

Wish he had mentioned this in the video - took me hours of experimentation and eventually got my answer on stackoverflow.