0

Shouldn't it perfectly legal to write private abstract methods in enums? Is there a reason to forbid it that I cannot see?

A toy code example that does not compile is below; Lets assume I do not want to commit to any api other than the singleton being multiple or not.

Note that this can be implemented without abstract methods but this is just an example. I am asking if there is a general reason to forbid it.

 enum Count{
    SINGLE { 
        private int howMany() {
            return 1;
        }
    },
    DOUBLE{ 
        private int howMany() {
            return 2;
        }
    };  
    private abstract int howMany();// !! this declaration is illegal
    public boolean isMultiple(){
        return this.howMany() > 1;
    }
}

public static void main (String... args){
    System.out.println(Arrays.toString(Count.values()));
    for(Count s : Count.values()){
        System.out.println(s.howMany() + " is " + (s.isMultiple() ? "many" : "one"));
    }
}

edit1: This is not asking why private abstract methods are forbidden in general. I am asking why use of it in enums is illegal providing an example that such use would make sense. This is not a duplicate. I couldn't find the answer in Stackoverflow. The answer to the general case does not apply here.

user2259824
  • 135
  • 8

0 Answers0