2

I currently use an enum defined as such :

public enum Language{
    English("english", "eng", "en", "en_GB", "en_US"),
    German("german", "de", "ge"),
    Croatian("croatian", "hr", "cro"),
    Russian("russian");

    private final List<String> values;

    Language(String ...values) {
        this.values = Arrays.asList(values);
    }

    public List<String> getValues() {
        return values;
    }
    public static Language find(String name) {
        for (Language lang : Language.values()) {
            if (lang.getValues().contains(name)) {
                return lang;
            }
        }
        return null;
    }
}

Thing is, I will have to use several enums using the same methods and constructor. The only thing changing would in my case be the values of the enum itself.

Is there a way for me to create a superclass of this enum that I could reuse ?

So that I could do something like :

public Language extends(specializedEnum){
        English("english", "eng", "en", "en_GB", "en_US"),
        German("german", "de", "ge"),
        Croatian("croatian", "hr", "cro"),
        Russian("russian");
}

Thank you by advance !

jlengrand
  • 12,152
  • 14
  • 57
  • 87
  • 3
    possible duplicate of [Java extend enum](http://stackoverflow.com/questions/1414755/java-extend-enum) – Keppil Aug 23 '12 at 12:17
  • 1
    You can only implement interfaces to ensure compatibility, at least. – Bernd Ebertz Aug 23 '12 at 12:19
  • Thx Keppil, but I already read that one. The values will NOT be the same, only the methods will – jlengrand Aug 23 '12 at 12:20
  • The answer still holds, you can't extend `enum`s, nor can an `enum` extend anything. It can however implement an `interface` if that helps. – Keppil Aug 23 '12 at 12:24

3 Answers3

9

There are difficulties with this in Java, but a solution that will give you a good deal of what you need is to make all your enums implement a common interface.

You'll have trouble reusing the code for the methods that implement the interface, but you can mitigate the damage by delegating the real work to outside methods.

Also, often a lookup-by-name function will be needed that gives you the right enum member regardless of the exact enum class. This can be accomplished using a separate HashMap that aggregates all enum members.

Basically, this is the outline of how I'm doing it in a project right now.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Thx Marko ! This seem rather complex though, but something (I think) quite easy for the mind. My main concern is to avoid code repetitions. But with 4 or 5 such enums only, I might as well just copy/paste :s – jlengrand Aug 23 '12 at 12:24
  • I have 6 enums I believe, and it pays very much to have this architecture because I absolutely need the dispatch on the type of the enum member. Nothing else can replace that benefit, so it would be only more code if I didn't use this. – Marko Topolnik Aug 23 '12 at 12:26
  • Thx for the help, you definitely put me on a right track. I think getting the job done from outside might be the cleanest solution – jlengrand Aug 23 '12 at 12:28
5

Enum can't be extended in Java.

Toilal
  • 3,301
  • 1
  • 24
  • 32
4

Its simply not possible to extend Enums but you could implement an common interface to add extra functionality.

public interface BaseLanguage {
   public void doStuff();
}

public enum Language implements BaseLanguage {
    English("english", "eng", "en", "en_GB", "en_US"),

   // etc.

   @Override
   public void doStuff() {
     // do work based on current Language enum
   }
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276