JAVA9 all to come out, JAVA8 new features are not clear, is not a bit behind OH ~
Interface Definition Enhancements
Prior to JDK1.8, the interface was defined:
Interface (English: Interface), which is an abstract type in the Java programming language, is a collection of abstract methods, and interfaces are usually declared with Interface. A class inherits the abstract method of an interface by inheriting the interface.
Prior to JDK1.8, the interface had the following features:
- Each method in the interface is implicitly abstract, and the methods in the interface are implicitly specified as public abstract(only public abstract, and other modifiers will be error-marked).
- The interface can contain variables, but the variables in the interface are implicitly specified as public static final variables (and can only be public, with the private modifier reporting compilation errors).
- The method in the interface is not implemented in the interface, only the class implementing the interface can implement the method in the interface.
And now, JDK1.8, break the routine, enhance the interface definition:
Allows you to define common methods in the interface, decorated with the default or static keywords. Of course, the definition of these two keywords is slightly different.
define the method with the DEFAULT keyword: Use the default keyword to define the method in the interface, you need to add a method body, the default is known as the meaning that the method provides a default written method body implementation. Of course, it is also possible to continue to overwrite it in a sub-interface.
1 Interfacemyinterface{2 voidMethod ();//How the method is originally defined in the interface3 4 default voidDmethod () {5SYSTEM.OUT.PRINTLN ("Default definition Method! ");6 }7 }8 classInterfaceImplImplementsmyinterface{9 Ten @Override One Public voidMethod () {//The method that does not use the default keyword definition, implements the interface, must overwrite the method in the interface ASystem.out.println ("Hello world!"); - } - the //@Override - //Public void Dmethod () { - //System.out.println ("Of course, can also go to overwrite dmethod!"); - // } + } - Public classMain { + Public Static voidMain (string[] args) { AInterfaceImpl Aninterface =NewInterfaceImpl (); at Aninterface.method (); -Aninterface.dmethod ();//Call the default method, output: "Default definition Method!" " - } -}
define the method with the Static keyword: we know that in Java, a method that is decorated with the static keyword is called a static method, and it can be accessed without relying on an object. The same is true of defining the static method in the interface, which can be called directly through the interface name.
1 Interfacemyinterface1{2 3 Static voidSmethod () {4SYSTEM.OUT.PRINTLN ("Static defined Method! ");5 }6 }7 Public classMain1 {8 Public Static voidMain (string[] args) {9Myinterface1.smethod ();//the static method is called directly by the interface name. Ten } One}
In summary, the meaning of using the default and static definition methods inside an interface is to avoid subclasses repeating the same code.
JAVA8 new Features-Interface definition enhancements