jdk1.8 -- interface

JDK1.8 adds new features to the definition of java interfaces, such as default implementation methods and static methods (or class methods). Here is a brief summary of the relevant features of java in jdk1.8 and how to use them.

 

Interface usage and precautions

 

Let's first look at a simple example:

 

public interface RenderService {
    //The default is public static final
    int pc_page = 0;
 
    //The default is public abstract
    void render();
 
    //default is public
   default void cache(){
        System.out.println("Interface default method: cache page");
    }
 
    //Static method, default is public
    static void cdn(){
        System.out.println("Interface static method: push cdn cache");
    }
}

 

 As you can see, the following four types of members can be defined in the interface:

1. Constants: The constants defined in the interface are modified by public static final by default (and can only be modified by public static final ) , that is, " global static constants " . It can be accessed directly through the class name (or interface name).

2. Common method: The common method defined in the interface is modified by public abstract by default ( and can only be modified by public abstract ) , and the method needs to be implemented by the implementing class.

3. Default method: The default method defined in the interface is modified by public by default (and can only be modified by public ). Since the default method has been provided, the implementation class can be used directly or rewritten.

4. Static method: An interface can be regarded as a special class, also called a class method, which is modified by public by default (and can only be modified by public ).

 

It should be noted that default static methods cannot be defined in interfaces .

 

 code test

 

The constants and static methods in the corresponding interface can be called directly through the interface name without creating an implementation class object. But if you want to use ordinary methods and default methods, you must create an implementation class object and call it through this object. For the above interface, you can test it through the following main method:

public class Main {
    public static void main(String[] args) {
        // use constant
        System.out.println("Interface constant: "+RenderService.pc_page);
        // use static method
        RenderService.cdn();
     
        RenderService renderService = new TestRenderService();
        // use default method
        renderService.cache();
        // use the implementation class method
        renderService.render();
    }
 
 
}
class TestRenderService implements RenderService{
    @Override
    public void render() {
        System.out.println("Subclass implementation method: rendering page");
    }
}

It can be seen in the test code that constants and static methods can be used directly through the RenderService class name, while the default methods and ordinary methods defined in the interface must be called through objects that implement the class TestRenderService .

 

 Summarize

 

The new features of the interface in Jdk1.8 are mainly to extract some features of the abstract class into the interface. In some cases, an interface can be used directly instead of an abstract class to make the use of the interface more flexible. But it cannot be said that interfaces can completely replace abstract classes, after all, their roles are different.

The role of an interface is to formulate a general specification (which is why all methods and constants in an interface are public ).

The role of abstract classes is to perform one-step public abstract extraction of ordinary classes.

 

Guess you like

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