Java interface modifier details

Source: Internet
Author: User

An interface provides a unified "protocol", and its attributes also belong to members of the "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. This satisfies the abstraction of interfaces and specific implementations.

 

Interfaces are an important magic weapon for building loosely coupled software systems.

 

A. interfaces are used to describe all services provided by the system. Therefore, member constants and methods in interfaces must be public to ensure that external users can access them;

 

B. The interface only describes what the system can do, but does not specify how to do it.

Are abstract methods;

 

C. The interface does not involve details related to any specific instance. Therefore, the interface has no constructor, cannot be instantiated, no instance variables, and only static variables.

 

D. the variables in the interface are shared by all implementation classes. Since they are shared, they must be unchanged, because they cannot be shared. Therefore, variables are of the final type, that is, constants.

 

E. Why can't variables be defined in interfaces? 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.

 

Therefore:

The method of the interface is public abstract by default.

A variable cannot be defined in the interface, that is, the final modification must be added before the defined variable to make it a constant (the variable without final modification will become a constant with final modification ). Therefore, the attribute of the interface is a public static final constant by default, and the initial value must be assigned.

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.