1

If I have the following interface and I want to implement it

public interface A<E extends Comparable<E>>{
    //code
}

What is the correct syntax for the implementing class declaration? I get an error when I do this

public class B<E extends Comparable<E>> implements A<E extends Comparable<E>>{}

Should it just read implements A<E> or just implements A?

2 Answers 2

3

Where the Comparable type E is, for example, String, you would want:

public class B implements A<String> { ... }

Where you want to retain the generic type parameter declaration in B, you would have:

public static class B<E extends Comparable<E>> implements A<E> { ... }

Note that the E in A is not related to the E in B, i.e. the following is valid:

public static class B<Foo extends Comparable<Foo>> implements A<Foo> { ... }

(whether you want to distinguish or not in your code I don't know, but it might help in understanding)

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

Comments

2
public class B<E extends Comparable<E>> implements A<E>{}

1 Comment

I think so too. You have to redeclare that this E is really a Comparable, before you can give it to your interface.

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.