The purpose of the interface in Java is to provide a programming framework that extends other classes as a uniform specification and is a very good design in Java.
The boy uses the following code to summarize the members that interfaces in Java can define and the keywords they are decorated by default:
//the access modifier for the external interface can only be public or the default modifier, and its members can only be decorated with the public access modifier, and the interface cannot be final decorated Public InterfaceA {//member variables, default with public static final decorationString name= "Ahei"; //member methods, default with public abstract decoration voidAbstractmethod (); //internal interface, default with public static decoration Interfaceinterf{}//inner class, default with public static decoration classinnerclass{}//static methods, which are modified by default with public, and static methods in the interface cannot be final decorated Static voidStaticmethod () {}//The default method, which provides a method implementation, as a means of library, frame forward compatibility default voidDefaultmethod () {System.out.println ("Haha"); } Public Static voidMain (string[] args) {//The following description of the class members and interface members in the interface are the default static decorated NewA.interf () {}; NewA.innerclass (); //The invocation of a static method is determined at compile time and is determined by the type of the declarationC c=NewD (); C.set (); //Output C//The default method is called through the object NewF (). Defaultmethod (); }}//static methods can also be overridden, but they belong to their own classes and do not have run-time polymorphismclassC {Static voidset () {System.out.println (C); }}classDextendsC {Static voidset () {System.out.println (D); }}classFImplementsa{@Override Public voidAbstractmethod () {}}
An enumeration class can also be defined in the Java interface, and the static and default methods in the interface are JAVA8 new, and the default method provides new functionality for the interface and does not affect the implementation of older versions of code, guaranteeing forward compatibility.
Definition of members in interfaces in Java