Interface default method of new features in Java 8

JAVA8 has been released for a long time, and there is no doubt that java8 is the most important version since java5 (released in 2004). These include new features in languages, compilers, libraries, tools, and JVM. The list of new features of Java 8 is as follows:

Interface default method
functional interface
Lambda expression
method reference
Stream
Optional class
Date API
Base64
repeated annotations and type annotations
01 interface default method
1. What is the interface default method
Starting from Java 8, the program allows the interface to include methods with specific implementations, Use default modification, this kind of method is the default method. You can add multiple default methods to the interface, and Java 8 provides many corresponding interface default methods.

2. The benefits of designing interface default methods. The advantage of
using interface programming is that the development is oriented to abstraction instead of specific oriented programming, making the program very flexible. The disadvantage is that when the interface needs to be modified, all implementations need to be modified at this time. The class of this interface, for example, before Java 8, there is no foreach method for our commonly used collection frameworks. The solution that can usually be thought of is to add new methods and implementations to the relevant interface in the JDK. Since Java8, the interface default method has been introduced. The benefits are obvious. First, the compatibility problem of the previous version of Java8 is solved. At the same time, for our future program development, you can also directly use the interface default method in the interface subclass. It is no longer necessary to implement response interface methods in each subclass.

3. In the default method
jdk8, the interface can contain implementation methods, which need to be modified by default. Such methods are called default methods. The default method must provide an implementation in the interface, and can be overridden on demand in the implementation class. The default method can only be called in the implementation class or through the implementation class object. Note: When the same default method exists in multiple parent interfaces, the child class inherits according to the principle of proximity.

public interface IMathOperation {
/**

  • Defining the interface default method to support method parameters
    */
    default void print(){
    System.out.println("This is the basic interface for numerical operations...");
    }
    }
    4. Static methods
    Static methods are methods modified by static. The static method page in the interface must also be implemented, providing a way to call methods directly through the interface.

public interface IMathOperation {
/**

  • Define the interface default method to support the method parameter
    */
    default void print(){
    System.out.println("This is the basic interface for numerical operations...");
    }

    /**

  • Define static default method
    */
    static void version(){
    System.out.println("This is a simple calculator of version 1.0");
    }
    }
    5. Use the interface default method to
    define the IMathOperation interface and provide the default printing method

public interface IMathOperation {
/**

  • Define the interface default method to support the method parameter
    */
    default void print(){
    System.out.println("This is the basic interface for numerical operations...");
    }

    /**

  • Define static default method
    */
    static void version(){
    System.out.println("This is a simple calculator of version 1.0");
    }
    /**
  • Integer addition method
  • @param
  • @param b
  • @return
    */
    public int add(int a,int b);
    }
    Subclass implementation
    definition MathOperationImpl subclass implements IMathOperation interface

As you can see here, you can choose to implement the default method of the interface when you implement the IMathOperation interface (or not implement it, use it directly in the subclass method).

public class MathOperationImpl implements IMathOperation { br/>@Override
public int add(int a, int b) {
// subclasses can directly call the parent class interface default method
IMathOperation.super.print();
// call the parent class static default Method
IMathOperation.version();
return a+b;
}
}
02 The case of multiple default methods
Use Java 8 to develop applications. When a subclass implements multiple interfaces, there may be multiple default methods for the interface default method definition, and the interface The default method may have the same name. At this time, three principles are usually followed when implementing or calling subclasses:

1. The method in the class has the highest priority

2. If the first item cannot be judged, then the priority of the sub-interface is higher: when the function signature is the same, the interface with the most specific implementation of the default method is preferred, that is, if B inherits A, then B is more specific than A

3. If it still cannot be judged, the class that inherits multiple interfaces must display the coverage and call the desired method, and explicitly choose which default method implementation to use

The sample code is as follows:

/**

  • Define the phone interface and provide the default info method
    */
    public interface Phone {
    default void info(){
    System.out.println("This is a phone");
    }
    }
    /**
  • Define the MiPhone sub-interface and inherit the Phone parent interface and also provide the info method
    */
    public interface MiPhone extends Phone{
    default void info(){
    System.out.println("This is a Xiaomi phone");
    }
    }
    /**
  • Implement the Phone MiPhone interface
    */
    public class M2sPhone implements Phone,MiPhone {
    public static void main(String[] args) {
    new M2sPhone().info();
    }
    }
    Print result:
    This is a Xiaomi phone

jdk8 has been released for a long time, and there is no doubt that java8 is the most important version since java5 (released in 2004). These include new features in languages, compilers, libraries, tools, and JVM.

Guess you like

Origin blog.51cto.com/14966610/2542497