Java developers should be able to java.lang.runnable,java.util.comparator,java.util.concurrent.callable And so on interface will not feel strange. They all have a single, abstract approach. For such an interface, we usually call the single abstract method interface (SAM,one abstract methodsInterface).
You should always use the following code snippet before
public class Inneranonymousclasssample {public static void Main (string[] args) { new Thread (new Runnable () { @Override public void Run () { System.out.println ("Anonymous Class Thread Demo")} ). Start (); }}
in the Java 8 , for this interface with a single abstract method, a name changed, called the function interface. So, this is not a new thing, the name is also for Lambada expression. let's take runnable for a look at the results of the close-up of the lambda expression.
public class Threadwithlambda {public static void Main (string[] args) { new Thread ((), System.out.println (" Thread with LAMBDA expression Demo "). Start ();} }
back to the function interface, Java introduced a @FunctionalInterface annotations. With it, the interface you define is a function interface that has only one abstract method, otherwise it will report a compilation error.
@FunctionalInterfacepublic interface Funinterfaceannotationdemo {public void Absmethoddemo (); public void Absmoremethoddemo ();}
Error: (81 Note Com.tr.learning.lambda.FunInterface.FunInterfaceAnnotationDemo is not a function interface Multiple non-overlay abstract methods found in interface Com.tr.learning.lambda.FunInterface.FunInterfaceAnnotationDemo
The above method can be introduced in Java 8 by the keyword default in the interface for one of the abstract method to specify a default implementation, so that the interface will become a legitimate function interface.
@FunctionalInterfacepublic interface Funinterfaceannotationdemo {public void Absmethoddemo (); Default public void Absmoremethoddemo () { System.out.println ("Default implementation of Method Ininterface"); }
Once you have the default keyword, the interface now seems to be an abstract class. It makes people feel weird. This default interface is primarily for backwards compatibility. When you add a method to an interface that has many subclasses, you don't want a subclass of subclasses to implement the method? This is a very real problem in the process of upgrading Java JDK. As for the use of interfaces with the default implementation or abstract classes, I think it is more appropriate to design the Java code or abstract classes.
Java 8 function interface, stale kind refurbished, just for lambda expression