New features of JDK8: static methods and default methods of interfaces

Before jdk8, variables and methods can be defined in the interface. Variables must be public, static, and final, and methods must be public and abstract. Since these modifiers are default, before JDK8, the following expressions are equivalent.

[java]  view plain copy  
 
  1. public interface JDK8BeforeInterface {  
  2.     public static final int field1 = 0;  
  3.   
  4.     int field2 = 0;  
  5.   
  6.     public abstract void method1(int a) throws Exception;  
  7.   
  8.     void method2(int a) throws Exception;  
  9. }  

 

 

JDK8 and later allows us to define static methods and default methods in interfaces.

[java]  view plain copy  
 
  1. public interface JDK8Interface {  
  2.   
  3.     // static modifier defines static method  
  4.     static void staticMethod() {  
  5.         System.out.println( "Static method in interface");  
  6.     }  
  7.   
  8.     // default modifier defines the default method  
  9.     default void defaultMethod() {  
  10.         System.out.println( "Default method in interface");  
  11.     }  
  12. }  


Then define an implementation class of the interface:

[java]  view plain copy  
 
  1. public class JDK8InterfaceImpl implements JDK8Interface {  
  2.     //After implementing the interface, because the default method is not an abstract method, it can not be rewritten, but it can also be rewritten if required for development  
  3. }  

 

 

Static methods can only be called through the interface name, not through the class name of the implementation class or the object of the implementation class. The default method can only be called by an object of the interface implementing class.

[java]  view plain copy  
 
  1. public class Main {  
  2.     public static void main(String[] args) {  
  3.         // static methods must be called through the interface class  
  4.         JDK8Interface.staticMethod();  
  5.   
  6.         //default method must be called by the object of the implementing class  
  7.         new JDK8InterfaceImpl().defaultMethod();  
  8.     }  
  9. }  


Of course, if the default method in the interface cannot meet the needs of an implementation class, then the implementation class can override the default method.

[java]  view plain copy  
 
  1. public class AnotherJDK8InterfaceImpl implements JDK8Interface {  
  2.       
  3.     // The signature is the same as the interface default method, but the default modifier cannot be added  
  4.     @Override  
  5.     public void defaultMethod() {  
  6.         System.out.println( "The interface implementation class overrides the default in the interface");  
  7.     }  
  8. }  



 

Since java supports that one implementation class can implement multiple interfaces, what if the same static and default methods exist in multiple interfaces? If the static methods in the two interfaces are exactly the same, and an implementation class implements both interfaces at the same time, no error will be generated at this time, because jdk8 can only call the static methods in the interface through the interface class, so the compiler can only call the static methods in the interface through the interface class. Said to be distinguishable. However, if the same default method is defined in two interfaces, and an implementation class implements both interfaces, the default method must be overridden in the implementation class, otherwise the compilation will fail.

[java]  view plain copy  
 
  1. public interface JDK8Interface1 {  
  2.   
  3.     // static modifier defines static method  
  4.     static void staticMethod() {  
  5.         System.out.println( "Static method in JDK8Interface1 interface");  
  6.     }  
  7.   
  8.     // default modifier defines the default method  
  9.     default void defaultMethod() {  
  10.         System.out.println( "The default method in the JDK8Interface1 interface");  
  11.     }  
  12.   
  13. }  

 

[java]  view plain copy  
 
  1. public class JDK8InterfaceImpl implements JDK8Interface,JDK8Interface1 {  
  2.   
  3.     // Since JDK8Interface is the same as the default method in JDK8Interface1, it must be overwritten here  
  4.     @Override  
  5.     public void defaultMethod() {  
  6.         System.out.println( "The interface implementation class overrides the default in the interface");  
  7.     }  
  8. }  

 

[java]  view plain copy  
 
  1. public class Main {  
  2.     public static void main(String[] args) {  
  3.         JDK8Interface.staticMethod();  
  4.         JDK8Interface1.staticMethod();  
  5.         new JDK8InterfaceImpl().defaultMethod();  
  6.     }  
  7. }  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344701&siteId=291194637