Member variables defined in interfaces in Java

Source: Internet
Author: User

All variables in the interface are public static final. So you can write:
Public static final int I = 10;
Or
Int I = 10; (part can be omitted)

Note that the initial value must be assigned to the variable during the declaration.

Explanation:

First, you need to understand the meaning of the interface. an interface provides a unified 'protocol', and its attributes also belong to members of 'protocol. they are public, static, and final constants. it is equivalent to a global constant.
Abstract classes are incomplete classes, which are equivalent to an intermediate layer between interfaces and specific classes. They satisfy the abstraction of interfaces and specific implementations.
If the interface can define variables, but the methods in the interface are abstract, the interface cannot modify attributes through behaviors. Some may say that it doesn't matter. You can modify the attributes of an interface by implementing the behavior of an interface object. Of course there is no problem, but consider this situation. If interface A has a static variable named a with the public access permission. According to the Java semantics, we can access variable A without implementing the interface object, and change the value of variable A in the interface through a. A = xxx. Just as this can be done in an abstract class, all objects implementing interface a will automatically own the value of a after this change, that is to say, a is changed in one place, the value of a in all these objects also changes. What is the difference between this and abstract classes? How does it reflect a higher abstract level of interfaces? How does it reflect the unified protocol provided by interfaces? What is the need for abstract interfaces? Therefore, there is no variable in the interface. If there is a variable, it is in conflict with the idea of unified abstraction provided by the interface. Therefore, the attribute in the interface must be a constant and can only be read but cannot be modified. In this way, a unified attribute can be provided for the object implementing the interface.

In layman's terms, what you think is to change is put in your own implementation, not in the interface. The interface is just a high-level abstraction of the attributes and behaviors of a class of things. It is open to modifications and extensions (different implementations of implements). interfaces are a manifestation of the open and closed principle.

All variables in the interface are public static final. So you can write:
Public static final int I = 10;
Or
Int I = 10; (part can be omitted)

Note that the initial value must be assigned to the variable during the declaration.

Explanation:

First, you need to understand the meaning of the interface. an interface provides a unified 'protocol', and its attributes also belong to members of 'protocol. they are public, static, and final constants. it is equivalent to a global constant.
Abstract classes are incomplete classes, which are equivalent to an intermediate layer between interfaces and specific classes. They satisfy the abstraction of interfaces and specific implementations.
If the interface can define variables, but the methods in the interface are abstract, the interface cannot modify attributes through behaviors. Some may say that it doesn't matter. You can modify the attributes of an interface by implementing the behavior of an interface object. Of course there is no problem, but consider this situation. If interface A has a static variable named a with the public access permission. According to the Java semantics, we can access variable A without implementing the interface object, and change the value of variable A in the interface through a. A = xxx. Just as this can be done in an abstract class, all objects implementing interface a will automatically own the value of a after this change, that is to say, a is changed in one place, the value of a in all these objects also changes. What is the difference between this and abstract classes? How does it reflect a higher abstract level of interfaces? How does it reflect the unified protocol provided by interfaces? What is the need for abstract interfaces? Therefore, there is no variable in the interface. If there is a variable, it is in conflict with the idea of unified abstraction provided by the interface. Therefore, the attribute in the interface must be a constant and can only be read but cannot be modified. In this way, a unified attribute can be provided for the object implementing the interface.

In layman's terms, what you think is to change is put in your own implementation, not in the interface. The interface is just a high-level abstraction of the attributes and behaviors of a class of things. It is open to modifications and extensions (different implementations of implements). interfaces are a manifestation of the open and closed principle.

Related Article

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.