Interfaces in Java (j2se entry 7)

Source: Internet
Author: User

Interfaces in Java

An interface is a program structure and a special abstract class. All methods in the interface must be public abstract methods, and all attributes in the interface must be public static final ).

Declare an InterfaceInterfaceKeyword, the interface is also a type, and the corresponding bytecode is generated after compilation,Its declaration specification must also comply with the type definition (one source file can only have one publicInterface, the interface name is the same as the source file name. If there is a public interface, the public class cannot be written).You do not need to add modifiers to attributes of an interface or methods.

The interface can also be inherited, but it can only be inherited by the interface. When the class is used for inheritance, the implements keyword should be used. In this case, the class and interface are not called the inheritance relationship, but the implementation relationship.Essentially inheritance.

A class can inherit or only inherit from another class,Multiple Interfaces can be implementedThe syntax is to write the interface name after implements. Multiple Interfaces are separated by commas.

Interfaces can be inherited from each other.The syntax is the same as the inheritance syntax of the class. When multiple interfaces are inherited, write the interface name after extends. If you want to inherit multiple interfaces, the interface names are separated by commas, the inheritance relationship of an interface only inherits the abstract methods in its parent interface to the subinterface. To implement an interface, you must implement all methods in the interface.

A class can inherit a class and implement one or more interfaces. The single inheritance restriction is bypassed by using interfaces..

The interface type can also be used as the compile-time type, but its actual runtime type must be an object instance of the class that fully implements the interface, so that polymorphism becomes flexible,

Note: When implementing an interface and implementing (overwriting) an abstract method, note that the public modifier must be added before the return value type of the method.If the method in the interface is not fully implemented (the compiler will report an error), this class can only be an abstract class and cannot be used to create objects. An interface is essentially a special abstract class. The interface does not have a constructor.

Meaning of the interface:
1. interfaces can implement multi-Inheritance.

2. mixed types (primary and secondary types) can be implemented through interfaces. in Java, Primary and Secondary types can be separated through interfaces. The primary type uses inheritance, secondary type, and interface implementation..

3. Interfaces further deepen the idea of standards. interfaces are a standard.To reduce coupling, the interface can separate the definition and implementation of methods, that is, separating the interface definer from the implementer, interfaces can also be used to reduce coupling between modules or systems. For interface programming, we can avoid differences between different implementations. What we see is only a good function,

Interface: defines the standard,
Interface implementation: Implementation Standard
Interface Caller: Standard usage
The interface programming principle is implemented according to the standard.


Interface callback: First define the interface (interface implementer) and then interface users. Finally, pass the interface implementation object to the user of the interface. the user of the interface calls the method of the interface implementer through the interface.

Interface callback: The interface definer defines the standard. The interface user first writes the code for use, and the interface implementer writes the implementation before passing the implementation object to the interface user. It calls the method in the interface, that is, the method in the interface implementation. This process is called interface callback.

Try to use the interface type as the compile-time type, and write the extracted common behavior in the interface as much as possible.

Replace a large interface with several small interfaces. (Interface isolation principle)


The function of a class is used as an interface to expose only the methods that you want to expose. The interface isolation principle can implement a higher level of encapsulation. The exposure methods vary depending on different objects.

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.