2

I'm looking to have a base enum like this...

public enum SessionState {

    WAITING(true), VOTING(true), PREPARING(false), PLAYING(false), ENDING(false);

    boolean newPlayersJoin;

    private SessionState(boolean newPlayersJoin){
        this.newPlayersJoin = newPlayersJoin;
    }

    public boolean canNewPlayersJoin(){
        return newPlayersJoin;   
    }

}

...and then another enum with the same values (WAITING, VOTING, PREPARING, PLAYING, ENDING) but with more functions. The reason I want a base enum without the extra methods is that some of the methods depend on functions that won't be included in that jar file (multi-module project)

My other enum would look something like this

public enum SessionStateCopy {

    WAITING(true, ... extra values), VOTING(true, ... extra values), PREPARING(false, ... extra values), PLAYING(false, ... extra values), ENDING(false, ... extra values);

    boolean newPlayersJoin;

    private SessionStateCopy(boolean newPlayersJoin, ... extra values){
        this.newPlayersJoin = newPlayersJoin;
    }

    public boolean canNewPlayersJoin(){
        return newPlayersJoin;   
    }

    ... extra methods

}
5
  • 1
    So what is your question? Commented Nov 8, 2013 at 18:13
  • You haven't asked a question. Also, read stackoverflow.com/questions/55375/add-values-to-enum Commented Nov 8, 2013 at 18:13
  • 1
    as for your requirement, i believe Enum is the wrong data type to use what you need is an abstract class or an interface Commented Nov 8, 2013 at 18:14
  • 1
    If your question is, whether enumerators can be inherited by extending another enum: No. Enums can just implement interfaces. Commented Nov 8, 2013 at 18:14
  • Are you expecting to store state information in the enum values? If so, that seems a little ... odd. Commented Nov 8, 2013 at 18:16

2 Answers 2

2

enums in Java are implicitly final, so you can't inherit them from each other. I'd have the two different enums, like you have in the OP, and add a couple of methods to convert between them:

public static SessionState convert(SessionStateCopy s) {
    return SessionState.valueOf(s.name());
}

public static SessionStateCopy convert(SessionState s) {
    return SessionStateCopy .valueOf(s.name());
}

Just to make sure you don't accidently add a value to one and not the other, I'd add a JUnit test to ensure this:

// imports snipped for clarity
public class SessionStateTest() {

    public void testMatchingMembers() {
        assertEquals("wrong number of values", SessionState.values().size(), SessionStateCopy.values().size());
        for (SessionState s: SessionState.values()) {
            assertEquals ("wrong value", s, convert(convert(s));
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

SessionStateCopy could take a SessionState in the constructor and perhaps vice versa. In that case the constants would be comparable to each other (though not in a switch). You can do something like if (someStateCopy.copy == SessionState.WAITING) {} if need be.
0

If I understand your question correctly, you want an enum to extend an enum.

You cannot.

However, you can have two enums implementing the same interface.

The one that's poorer in functionality would implements all methods but do nothing.

The other one would provide a "useful" implementation of the interface's methods.

For instance:

enum Foo { // "extends Bar" would not compile
    FOO();
}
interface Blah {
    void meh();
}
enum Bar implements Blah {
    SOMETHING();
    public void meh() {
        // nope     
    }
}
enum GreatBar implements Blah {
    SOMETHING();
    public void meh() {
        System.out.println("I'm doing something here!");
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.