Java 8 function interface, stale kind refurbished, just for lambda expression

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.