-1

I have this abstract class

public abstract class AbstractA<T extends Comparable<? super T>> {
    protected T value;
}

and my class

public class A<T> extends AbstractA {
}

I want to use the compareTo function in class a

But java gives me the error:

The method compareTo(capture#5-of ? super capture#4-of ?) in the type Comparable is not applicable for the arguments (Comparable)

the function looks like:

protected find(Comparable value){
    if( this.value.compareto(value) == 0){
    //
    }
}

But the function should be Called with (T value)

How do I have to extend the abstract class?

Julian
  • 9
  • 2

1 Answers1

0

To extend it you need to specify a generic interface that implements Comparable. For example:

public class A<T extends Comparable<? super T>> extends AbstractA<T> {
    // ...
}

Or a concrete class:

public class A extends AbstractA<String> {
    // ...
}

AbstractA does not implement Comparable. If you want A to implement Comparable you need to add it as an implements clause. E.g.

public class A extends AbstractA<String> implements Comparable<String> {

    @Override
    public int compareTo(String arg0) {
        return 0;
    }
}

To understand generics, there is a good Java tutorial.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66