Is it possible to extend an Enum?

allelopath
allelopath used Ask the Experts™
on
Doesn't look like it is. I want to do this:

package enumexample;

public enum MyEnum {
	
    ONE("One"),
    TWO("Two"),
    THREE("Three");

    String enumType;

    private MyEnum(String enumType) {
        this.enumType = enumType;
    }

    public String getEnumType() {
        return enumType;
    }

    @Override
    public String toString() {
        return enumType;
    }

    public static MyEnum fromString(String string) {
    	
        if (string==null) {
            return null;
        }
        else {
            if(string.equals("One")){
                return ONE;
            }
            else if(string.equals("Two")){
                return TWO;
            }
            else if(string.equals("Three")){
                return THREE;
            }
            else {
                return null;
            }
        }
    }
}

package enumexample;

public enum MyExtendedEnum extends MyEnum {
	
    FOUR("Four"),

    public static MyExtendedEnum fromString(String string) {
    	
        if (string==null) {
            return null;
        }
        else {
            if(string.equals("One")){
                return ONE;
            }
            else if(string.equals("Two")){
                return TWO;
            }
            else if(string.equals("Three")){
                return THREE;
            }
            else if(string.equals("Four")){
                return FOUR;
            }            else {
                return null;
            }
        }
    }
}

Open in new window


But it doesn't allow extending MyEnum.
Any suggestions?

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Awarded 2011
Awarded 2011

Commented:
I think there is type enum

http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

and Java Class Enum

I guess you can extend class but not type enum

Your MyEnum is value of type enum - you can't extend it

Awarded 2011
Awarded 2011

Commented:
This is class Enum - forgot to nmention this link:
http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Enum.html
Awarded 2011
Awarded 2011

Commented:
Awarded 2011
Awarded 2011

Commented:
So it looks like you cannot extend class Enum either
Awarded 2011
Awarded 2011
Commented:
Awarded 2011
Awarded 2011

Commented:


Maybe you'll like this suggestion:
http://www.vlad-yatsenko.eu/2011/01/java-magic-extending-enumerations/

It all seems too abstract to my mind, but seems close to what you wanted.

Author

Commented:
So, either of your last 2 posts show that the way to do this is to use an interface.
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial