Java enum class enum, defining static variables can also have abstract methods, but enum classes cannot be inherited

Source: Internet
Author: User

Reference 55049192

This explains why an enumeration class cannot be inherited: it has inherited the parent enum class, and its parent implements the serializable interface, which means that all enum classes implement serialization

The Enum class also cannot be inherited for the following reasons:

The public enum class name {variable list is separated by commas, and the last variable ends with a semicolon; List of available functions}

In fact, the enumeration class itself is the public final class enum class name {The type of the variable list is that class type and also uses the final keyword}, and the enumeration class comes with the declaration/definition of the value (), the valueof () function, and the static{} static code block.

So variables in the variable list can be written in three ways: 1. Variable name. Equivalent to defining a static variable with an enumeration class type

2. Variable name (variable name corresponds to value). A variable is defined with an enumeration class type, but the enumeration class has a private parameter constructor method and a private variable to use as a parameter, and the value of the parameter is the value of the variable name.

3. Variable name (variable name corresponding value) {Static code block}//except 2, the enumeration class also defines an abstract method, and the code block after the variable name corresponds to an anonymous inner class that implements the abstract method. As follows: Variable type variable name =new Enum class class name (parameter) {@Override

Public enum class type abstract class class name () {
return variable name;}

}

Java enum class enum, defining static variables can also have abstract methods, but enum classes cannot be inherited

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.