Define member variable description in Java interface

Source: Internet
Author: User

First you need to understand what the interface means. An interface is a uniform ' protocol ', and the attributes in the interface are members of the ' protocol '. They are public, static, and final constants. Equivalent to a global constant.


The variables inside the interface are public static final. So you can write this:

public static final int i=10;

Dolly Mix

int i=10; (You can omit a part, the member variable in the interface defaults to public static final)

Notice that the variable is given an initial value at the time of declaration.

Explain:

First you need to understand what the interface means. An interface is a uniform ' protocol ', and the attributes in the interface are members of the ' protocol '. They are public, static, and final constants. Equivalent to a global constant.

An abstract class is a class that is not ' complete ', equivalent to an interface and a middle layer of a concrete class. That is, to satisfy the abstraction of the interface and to satisfy the concrete implementation.

If an interface can define a variable, but the method in the interface is abstract, the property cannot be modified by behavior in the interface. Some people will say, no relationship, you can modify the properties of an interface by implementing the behavior of the interface's objects. This is of course not a problem, but consider such a situation. If interface A has a static variable A that has public access permissions. According to Java semantics, we can change the value of variable A in the interface without accessing the variable A through the object that implements the interface, through a.a = xxx; As can be done in an abstract class, all objects that implement interface A will automatically have the value of the changed a, which means that a place has changed a, and the value of a in all these objects has changed. What is the difference between this and the abstract class, how to embody the higher level of abstraction of the interface, how to embody the unified protocol provided by the interface, that also interface this abstraction to do? Therefore, the interface can not appear in the variable, if there are variables, and the interface provided by the unified abstraction of the idea is inconsistent. Therefore, the attribute in the interface must be constant, can only be read and cannot be changed, so as to provide a uniform property for the object that implements the interface.

In layman's words, what you think is to change is put in your own implementation, not in the interface, interface is just a kind of things of the properties and behavior of a higher level of abstraction. For the modification to close, the extension (different implementation implements) Open, interface is a manifestation of the principle of closure.


Define member variable description in Java interface

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.