Method default (default method) interface

First, the concept

1. The interface provides a default implementation of the method, and does not enforce classes override this method

2. The default method uses the default keyword to modify

 

Second, the introduction of background

1. When an interface to add new methods need to rewrite all of the implementation classes have new methods affected the existing implementation class, could cause the application to crash

2. The default method can not be forced to rewrite, it will not affect the existing implementation class

3. The method of example foreach Iterable interface is a default method, the object parameter is a Consumer

 

Third, the use

1. When an implementation class implements multiple interfaces, multiple interfaces are in the same default method, the implementation class must override the default method, otherwise the compilation error

a. rewriting their logic implementation class

b. implementation class using the super keyword to specify which interface to use the default method

 

2. Interface static methods

Interface supports defining static method, the default method is the default keyword can be replaced by static

 

Use the default interface method 3. After inheriting class can be used as static methods in an interface, you can create objects

 

 

 

reference:

https://www.jianshu.com/p/a58b6f5b0c54

https://www.jb51.net/article/137630.htm

 

Guess you like

Origin www.cnblogs.com/june0816/p/11523325.html