What would be different in Java if Enum declaration didn't have the recursive part

Posted by atamur on Stack Overflow See other posts from Stack Overflow or by atamur
Published on 2010-06-18T07:36:32Z Indexed on 2010/06/18 7:43 UTC
Read the original article Hit count: 270

Filed under:
|
|

Please see http://stackoverflow.com/questions/211143/java-enum-definition and http://stackoverflow.com/questions/3061759/why-in-java-enum-is-declared-as-enume-extends-enume for general discussion. Here I would like to learn what exactly would be broken (not typesafe anymore, or requiring additional casts etc) if Enum class was defined as

public class Enum<E extends Enum> 

I'm using this code for testing my ideas:

interface MyComparable<T> {
    int myCompare(T o);
}

class MyEnum<E extends MyEnum> implements MyComparable<E> {
    public int myCompare(E o) { return -1; }
}

class FirstEnum extends MyEnum<FirstEnum> {}

class SecondEnum extends MyEnum<SecondEnum> {}

With it I wasn't able to find any benefits in this exact case.

PS. the fact that I'm not allowed to do

class ThirdEnum extends MyEnum<SecondEnum> {}

when MyEnum is defined with recursion is
a) not relevant, because with real enums you are not allowed to do that just because you can't extend enum yourself
b) not true - pls try it in a compiler and see that it in fact is able to compile w/o any errors

PPS. I'm more and more inclined to believe that the correct answer here would be "nothing would change if you remove the recursive part" - but I just can't believe that.

© Stack Overflow or respective owner

Related posts about java

Related posts about generics