Why can't a class extend an enum?

后端 未结 5 1828
死守一世寂寞
死守一世寂寞 2021-01-17 10:18

I am wondering why in the Java language a class cannot extend an enum.

I\'m not talking about an enum extending an enum<

5条回答
  •  天涯浪人
    2021-01-17 10:50

    The whole point of an enum is to create a closed set of possible values. This makes it easier to reason about what a value of that enum type is -- easier for you the programmer, and also easier for the compiler (this closedness is what lets it handle enums efficiently in switches, for instance). Allowing a class to extend an enum would open up the set of possible values; at that point, what does the enum buy you that a regular class wouldn't?

提交回复
热议问题