What makes the Enum class uninheritable?

后端 未结 1 345
盖世英雄少女心
盖世英雄少女心 2021-01-19 04:35

I\'ve been looking at the source code of the Enum class. It seems like a plain abstract class with a protected constructor to me. It\'s not final, it doesn\'t have any speci

1条回答
  •  盖世英雄少女心
    2021-01-19 05:04

    It's only invalid to extend Enum directly within the language. Any time you declare an enum, that does create a subclass of Enum.

    The way the JLS prevents you from "manually" declaring a class which extends Enum is simply to prohibit it explicitly. From JLS section 8.1.4:

    It is a compile-time error if the ClassType names the class Enum or any invocation of it.

    (Where ClassType is the type you're extending.)

    Without that rule, your code would have been perfectly valid.

    0 讨论(0)
提交回复
热议问题