3

I am new to java. I am just trying to pass Comparable<String> into a method parameter of generic type <E extends Comparable<E>> . I believe the meaning of <E extends Comparable<E>> is any object that extends Comparable. Please let me know how to pass Comparable<String> or any object that extends Comparable<String> and has an other object in it.

Compiler is giving me error The inferred type Compare<String> is not a valid substitute for the bounded parameter <E extends Comparable<E>>

Code:

public class Compare<T> implements Comparable<T>{

    public int compareTo(T o) {

        return 0; // Not worried about logic

    }

}

class CompareTest{

    public <E extends Comparable<E>>void testGeneric(E e){

        System.out.println("Executed");
    }

    public static void main(String args[]){
        Compare<String> compare = new Compare<String>();

        CompareTest test = new CompareTest();
        test.testGeneric(compare);
        //The inferred type Compare<String> is not a valid substitute for the bounded
        //parameter <E extends Comparable<E>>
    }
}

4 Answers 4

3

E extends Comparable<E> means: a type E that is able to compare to other objects of the same type E.

But your Compare type doesn't qualify. It can't compare with another Compare. A Compare<T> can only compare itself to a T, and not to a Compare<T>, since it's declared as

public class Compare<T> implements Comparable<T>

It's quite hard to understand what you want to achieve with this Compare type.

Sign up to request clarification or add additional context in comments.

1 Comment

I was just practicing the notion < E extends Comparable< E > > . I though If I pass an object like new Comparable<String>() it would accept as It Comparable< Strinng > extends Comparable. Not sure where am I missing.
2

Your method

public <E extends Comparable<E>> void testGeneric(E e){

expects a type E that is a sub type of Comparable<E>. But you are passing it a Compare<String> which is not a sub type of <Comparable<Compare<String>>, but a sub type of Comparable<String>.

You'll have to clarify what you are trying to do if you need more help.

Comments

1

The error in my IDE says:

Inferred type 'Compare<java.lang.String>' for type parameter 'E' is not within its bound; 
should implement 'java.lang.Comparable<Compare<java.lang.String>>'

It seems that E is inferred as Compare<String> instead of String. To get E to be String, try this:

public <E extends Comparable<E>> void testGeneric(Comparable<E> e){

1 Comment

it worked like a heaven. I was expecting < E extends Comparable< E > > to expect Compare< String > and match Compare < String > . I have no clue how come Compare< String > = new Compare < String >(); is being treated Comparable<Compare<String>> instead of Compare< String >
1

To define a class as being comparable, the generic parameter to Comparable must be the class itself:

public class MyComparable implements Comparable<MyComparable> {
    public int compareTo(MyComparable o) {
        return 0;
    }
}

Applying that to your class, you get the following code (which compiles):

public static class Compare<T> implements Comparable<Compare<T>> {
    public int compareTo(Compare<T> o) {
        return 0; // Not worried about logic
    }
}

class CompareTest {
    public <E extends Comparable<E>> void testGeneric(E e) {
        System.out.println("Executed");
    }
}

public static void main(String[] args) {
    Compare<String> compare = new Compare<String>();
    CompareTest test = new CompareTest();
    test.testGeneric(compare);
}

2 Comments

you are absolutely right. It was the problem of being able to create an object of type you created in the seconds code block. i.e. MyComparable < MyComparable < MyComparable so on and on ... > > . Can't we create an object of class MyComparable in the second block. If so an example of that would be highly appreciated.
I have simplified my answer for clarity and provided you with a repaired (ie compilable) version of your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.