0

Please, help me understand why enum classes can't be extended. This code: `

public class Main
{
    public static void main(String[] args) {
        System.out.println(B.C);
    }
    
    public enum A {
        A, B, C
    }
    
    public enum B extends A {
        C, D, E
    }
}

` Will produce compile exception:

error: '{' expected
    public enum B extends A {
                 ^

But why? Is enum class isn't regular class?

But enum can implement interfaces... In my mind the only option why this code don't compile is that emun classes are final, is it true?

  • 1
    They're final, and they each already have a class they extend *directly*: `java.lang.Enum` – ernest_k Dec 23 '22 at 07:31
  • 1
    It doesn't compile because the language is designed this way. What would `enum B extends A` *mean*? Please see [this](https://meta.stackoverflow.com/a/323382/5133585) and [this](https://meta.stackoverflow.com/a/293819/5133585), and clarify what you mean by "why". – Sweeper Dec 23 '22 at 07:31
  • 2
    @ernest_k Not necessarily though. They are not final if the enum constants have class bodies. – Sweeper Dec 23 '22 at 07:33
  • 1
    Think it's the same subject as here : https://stackoverflow.com/q/1414755/7563004 – Emerald Cottet Dec 23 '22 at 07:39
  • See also the difference between `extends` and `implements` here https://stackoverflow.com/a/10839155/7563004 – Emerald Cottet Dec 23 '22 at 07:56

0 Answers0