1

I am facing an issue regarding extending a method from abstract which returns enum.

Below abstract class from com.fasterxml.jackson library which I need to extend.

public abstract class JsonSchema
{
    public abstract JsonFormatTypes getType();
}

And JsonFormatTypes is enum:

public enum JsonFormatTypes
{
    STRING,
    NUMBER,
    INTEGER,
    BOOLEAN,
    OBJECT,
    ARRAY,
    NULL,
    ANY;
}

Now I want extend JsonSchema clas for SwitchSchema and JsonFormatTypes to Have Type as "SWITCH"

public class SwitchSchema extends JsonSchema
{
    @Override
    public JsonFormatTypes getType(){
        return JsonFormatTypes.SWITCH;
    }
}

Now my question is how to extend JsonFormatTypes enum to have "SWITCH" and use it in overridden method ?

Abhij
  • 1,222
  • 4
  • 19
  • 37
  • you can't extend an enum, but you can extend classes. why don't you convert your enum to a class? – Leo Jun 15 '16 at 13:06

1 Answers1

4

You cannot extend an enum.

From the docs:

All enums implicitly extend java.lang.Enum. Because a class can only extend one parent (see Declaring Classes), the Java language does not support multiple inheritance of state (see Multiple Inheritance of State, Implementation, and Type), and therefore an enum cannot extend anything else.

Despite some online literature (e.g. here), the switch statement is not currently supported by JSON, hence JsonFormatTypes (from Jackson, I suppose) is fine as is, and you probably don't want to use it any differently.

If you're trying to make a new specification for the JSON format, you might be better off using your own parser instead of relying on a third-party.

Mena
  • 47,782
  • 11
  • 87
  • 106