13

Consider this code:

public interface Foo extends Comparable<Foo> {}

public enum FooImpl implements Foo {}

Due to the restrictions of type erasure, I receive the following error:

java.lang.Comparable cannot be inherited with different arguments: <Foo> and <FooImpl>

I have the following requirements:

  • FooImpl needs to be an enum, because I need to use it as a default value in annotations.
  • The contract of my interface is that it needs to be comparable.

I already tried using generic bounds in the interface, but this is not supported in Java.

11
  • Try public interface Foo public enum FooImpl implements Foo, Comparable<FooImpl> {...} Commented Aug 23, 2011 at 12:28
  • The problem is that FooImpl already implements Comparable<FooImpl>. Commented Aug 23, 2011 at 12:33
  • And you just want to show that it does? Write it in a comment. Commented Aug 23, 2011 at 12:45
  • I need to be able to treat instances of Role as Comparable, a comment won't help here. Commented Aug 23, 2011 at 12:48
  • Just above public enum..., add: //Note: implements Comparable. Commented Aug 23, 2011 at 12:50

3 Answers 3

13

Enums implement Comparable, so FooImpl ends up extending Comparable twice with incompatible arguments.

The following will work:

public interface Foo<SelfType extends Foo<SelfType>> extends Comparable<SelfType> { ... }

public enum FooImpl implements Foo<FooImpl> { ... }
Sign up to request clarification or add additional context in comments.

1 Comment

just public interface Foo<SelfType> extends Comparable<SelfType> is sufficient
12

Enum already implements comparable so you can't override it.

A general answer regarding why-would-an-enum-implement-an-interface.

3 Comments

You're avoiding the question. What if it was another interface.
@aioobe huh ? The question asks how to implement an interface that extends comparable
If I want to refer to my enum object via my interface: (Interface Foo {...} Enum1 implements Foo {...} Foo x1 = Enum1.FIRST;) and if I also need to use it in a context that requires a Comparable, then I have to declare that Foo extends Comparable<??something??>. Without the (admittedly clunky) typing in @jvdnese's answer, you cannot have two different Enums implement Foo and let the compiler know Foos are Comparable. (It's only a warning, so you can get away with just skipping implements Comparable). An example with the warning is TreeSet<Foo> x = new TreeSet<>();
1

Actually the error you will get is :

The interface Comparable cannot be implemented more than once with different arguments : Comparable<FooImpl> and Comparable<Foo>

As enum FooImpl already implementing Comparable<FooImpl> implicitly, you can not override it again as Comparable<Foo>.

Comments

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.