Home > Java > Javagetting Started > body text

Can enumeration classes be inherited in java?

青灯夜游
Release: 2019-12-30 18:02:59
Original
7897 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!