Home >Java >Javagetting Started >Can enumeration classes be inherited in java?
java enumeration class
In java, use the keyword enum to define the enumeration class, enumeration class It is a special class. Most of its functions are the same as ordinary classes. The difference is:
● The enumeration class inherits the java.lang.Enum class instead of the default Object class. The java.lang.Enum class implements the java.lang.Serializable and java.lang.Comparable interfaces.
● Non-abstract enumeration classes will use final modification by default, so subclasses cannot be derived.
Can enumeration classes in Java be inherited?
Enumeration classes in Java cannot be inherited.
After the enumeration class is defined using enum, it inherits the java.lang.Enum class by default after compilation, instead of inheriting the Object class. The enum declaration class inherits the two interfaces Serializable and Comparable. And after using enum declaration, the class will be added with final declaration (same as String) by the compiler, so this class cannot be inherited.
The internally defined enumeration values of an enumeration class are instances of the class (and must be defined on the first line. When the class is initialized, these enumeration values will be instantiated).
The new enum keyword in Java 5 can define enumeration classes. This class is a special class that can define its own fields, methods, implement interfaces, and also define its own constructor.
Recommended learning: Java video tutorial
The above is the detailed content of Can enumeration classes be inherited in java?. For more information, please follow other related articles on the PHP Chinese website!