What is use of Functional Interface in Java 8?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








125















I came across a new term named Functional Interface in Java 8.



I could only find one use of this interface while working with lambda expressions.



Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make use of the @FunctionalInterface annotation. It will allow us to declare only a single method in the interface.



For example:



@FunctionalInterface
interface MathOperation
int operation(int a, int b);



How useful it is in Java 8 other than just working with lambda expressions?



The question here is different from the one I asked. It is asking why we need Functional Interface while working with Lambda expression. My question is: why using Functional Interface other than directly with lambda expressions?










share|improve this question



















  • 1





    It looks duplcate to this link. They also talk about why there should be only one method in Functional Interface. stackoverflow.com/questions/33010594/…

    – Kulbhushan Singh
    Apr 27 '16 at 6:29






  • 1





    @KulbhushanSingh I saw this question before posting... Both questions sense difference...

    – Madhusudan
    Apr 27 '16 at 6:32











  • check out a detailed explanation on functional interfaces and changes on interfaces of java 8: sysdotoutdotprint.com/index.php/2017/04/22/java-8-interface

    – mel3kings
    Apr 22 '17 at 0:36

















125















I came across a new term named Functional Interface in Java 8.



I could only find one use of this interface while working with lambda expressions.



Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make use of the @FunctionalInterface annotation. It will allow us to declare only a single method in the interface.



For example:



@FunctionalInterface
interface MathOperation
int operation(int a, int b);



How useful it is in Java 8 other than just working with lambda expressions?



The question here is different from the one I asked. It is asking why we need Functional Interface while working with Lambda expression. My question is: why using Functional Interface other than directly with lambda expressions?










share|improve this question



















  • 1





    It looks duplcate to this link. They also talk about why there should be only one method in Functional Interface. stackoverflow.com/questions/33010594/…

    – Kulbhushan Singh
    Apr 27 '16 at 6:29






  • 1





    @KulbhushanSingh I saw this question before posting... Both questions sense difference...

    – Madhusudan
    Apr 27 '16 at 6:32











  • check out a detailed explanation on functional interfaces and changes on interfaces of java 8: sysdotoutdotprint.com/index.php/2017/04/22/java-8-interface

    – mel3kings
    Apr 22 '17 at 0:36













125












125








125


31






I came across a new term named Functional Interface in Java 8.



I could only find one use of this interface while working with lambda expressions.



Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make use of the @FunctionalInterface annotation. It will allow us to declare only a single method in the interface.



For example:



@FunctionalInterface
interface MathOperation
int operation(int a, int b);



How useful it is in Java 8 other than just working with lambda expressions?



The question here is different from the one I asked. It is asking why we need Functional Interface while working with Lambda expression. My question is: why using Functional Interface other than directly with lambda expressions?










share|improve this question
















I came across a new term named Functional Interface in Java 8.



I could only find one use of this interface while working with lambda expressions.



Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make use of the @FunctionalInterface annotation. It will allow us to declare only a single method in the interface.



For example:



@FunctionalInterface
interface MathOperation
int operation(int a, int b);



How useful it is in Java 8 other than just working with lambda expressions?



The question here is different from the one I asked. It is asking why we need Functional Interface while working with Lambda expression. My question is: why using Functional Interface other than directly with lambda expressions?







java lambda interface java-8






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 11 '18 at 13:46









Ahmad Al-Kurdi

1,32721027




1,32721027










asked Apr 27 '16 at 6:21









MadhusudanMadhusudan

1,61962965




1,61962965







  • 1





    It looks duplcate to this link. They also talk about why there should be only one method in Functional Interface. stackoverflow.com/questions/33010594/…

    – Kulbhushan Singh
    Apr 27 '16 at 6:29






  • 1





    @KulbhushanSingh I saw this question before posting... Both questions sense difference...

    – Madhusudan
    Apr 27 '16 at 6:32











  • check out a detailed explanation on functional interfaces and changes on interfaces of java 8: sysdotoutdotprint.com/index.php/2017/04/22/java-8-interface

    – mel3kings
    Apr 22 '17 at 0:36












  • 1





    It looks duplcate to this link. They also talk about why there should be only one method in Functional Interface. stackoverflow.com/questions/33010594/…

    – Kulbhushan Singh
    Apr 27 '16 at 6:29






  • 1





    @KulbhushanSingh I saw this question before posting... Both questions sense difference...

    – Madhusudan
    Apr 27 '16 at 6:32











  • check out a detailed explanation on functional interfaces and changes on interfaces of java 8: sysdotoutdotprint.com/index.php/2017/04/22/java-8-interface

    – mel3kings
    Apr 22 '17 at 0:36







1




1





It looks duplcate to this link. They also talk about why there should be only one method in Functional Interface. stackoverflow.com/questions/33010594/…

– Kulbhushan Singh
Apr 27 '16 at 6:29





It looks duplcate to this link. They also talk about why there should be only one method in Functional Interface. stackoverflow.com/questions/33010594/…

– Kulbhushan Singh
Apr 27 '16 at 6:29




1




1





@KulbhushanSingh I saw this question before posting... Both questions sense difference...

– Madhusudan
Apr 27 '16 at 6:32





@KulbhushanSingh I saw this question before posting... Both questions sense difference...

– Madhusudan
Apr 27 '16 at 6:32













check out a detailed explanation on functional interfaces and changes on interfaces of java 8: sysdotoutdotprint.com/index.php/2017/04/22/java-8-interface

– mel3kings
Apr 22 '17 at 0:36





check out a detailed explanation on functional interfaces and changes on interfaces of java 8: sysdotoutdotprint.com/index.php/2017/04/22/java-8-interface

– mel3kings
Apr 22 '17 at 0:36












10 Answers
10






active

oldest

votes


















108














@FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface.



But you can use lambdas without this annotation as well as you can override methods without @Override annotation.



From docs




a functional interface has exactly one abstract method. Since default
methods have an implementation, they are not abstract. If an interface
declares an abstract method overriding one of the public methods of
java.lang.Object, that also does not count toward the interface's
abstract method count since any implementation of the interface will
have an implementation from java.lang.Object or elsewhere




This can be used in lambda expression:



public interface Foo 
public void doSomething();



This cannot be used in lambda expression:



public interface Foo 
public void doSomething();
public void doSomethingElse();



But this will give compilation error:



@FunctionalInterface
public interface Foo
public void doSomething();
public void doSomethingElse();




Invalid '@FunctionalInterface' annotation; Foo is not a functional
interface







share|improve this answer




















  • 35





    To be more precise, you have to have exactly one abstract method that doesn’t override a method in java.lang.Object in a functional interface.

    – Holger
    Apr 27 '16 at 8:04






  • 1





    @Holger Yeah, that's exactly what the docs says.

    – Sergii Bishyr
    Apr 27 '16 at 8:07






  • 6





    …and it’s slightly different to “not have more than one public method besides static and default”…

    – Holger
    Apr 27 '16 at 8:12







  • 2





    @Holger you are right. I have added quote from docs to my answer. Thanx! :)

    – Sergii Bishyr
    Apr 27 '16 at 8:16






  • 1





    Foo foo = o -> obj.equals(anyObj); should work.

    – lwpro2
    Jul 23 '17 at 15:25



















9














Not at all. Lambda expressions are the one and only point of that annotation.






share|improve this answer


















  • 6





    Well, lamdbas work without the annotation as well. It's an assertion just like @Override to let the compiler know that you intended to write something that was "functional" (and get an error if you slipped).

    – Thilo
    Apr 27 '16 at 6:34







  • 1





    Straight to the point and the correct answer, though a bit short. I took the time to add a more elaborated answer saying the same thing with more words…

    – Holger
    Apr 27 '16 at 8:41


















9














The documentation makes indeed a difference between the purpose




An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.




and the use case




Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.




whose wording does not preclude other use cases in general. Since the primary purpose is to indicate a functional interface, your actual question boils down to “Are there other use cases for functional interfaces other than lambda expressions and method/constructor references?”



Since functional interface is a Java language construct defined by the Java Language Specification, only that specification can answer that question:



JLS §9.8. Functional Interfaces:






In addition to the usual process of creating an interface instance by declaring and instantiating a class (§15.9), instances of functional interfaces can be created with method reference expressions and lambda expressions (§15.13, §15.27).




So the Java Language Specification doesn’t say otherwise, the only use case mentioned in that section is that of creating interface instances with method reference expressions and lambda expressions. (This includes constructor references as they are noted as one form of method reference expression in the specification).



So in one sentence, no, there is no other use case for it in Java 8.






share|improve this answer






























    6














    As others have said, a functional interface is an interface which exposes one method. It may have more than one method, but all of the others must have a default implementation. The reason it's called a "functional interface" is because it effectively acts as a function. Since you can pass interfaces as parameters, it means that functions are now "first-class citizens" like in functional programming languages. This has many benefits, and you'll see them quite a lot when using the Stream API. Of course, lambda expressions are the main obvious use for them.






    share|improve this answer






























      5














      A lambda expression can be assigned to a functional interface type, but so can method references, and anonymous classes.



      One nice thing about the specific functional interfaces in java.util.function is that they can be composed to create new functions (like Function.andThen and Function.compose, Predicate.and, etc.) due to the handy default methods they contain.






      share|improve this answer























      • You should elaborate on this comment more. What about method references and new functions?

        – K.Nicholas
        Mar 14 '18 at 22:13


















      4














      An interface with only one abstract method is called Functional Interface.
      It is not mandatory to use @FunctionalInterface, but it’s best practice to use it with functional interfaces to avoid addition of extra methods accidentally. If the interface is annotated with @FunctionalInterface annotation and we try to have more than one abstract method, it throws compiler error.



      package com.akhi;
      @FunctionalInterface
      public interface FucnctionalDemo

      void letsDoSomething();
      //void letsGo(); //invalid because another abstract method does not allow
      public String toString(); // valid because toString from Object
      public boolean equals(Object o); //valid

      public static int sum(int a,int b) // valid because method static

      return a+b;

      public default int sub(int a,int b) //valid because method default

      return a-b;







      share|improve this answer
































        2














        @FunctionalInterface is a new annotation are released with Java 8 and provide target types for lambda expressions and it used on compilation time checking of your code.



        When you want to use it :



        1- Your interface must not have more than one abstract methods, otherwise compilation error will be given.



        1- Your interface Should be pure, which means functional interface is intended to be implemented by stateless classes, exmple of pure is Comparator interface because its not depend on the implementers state, in this case No compilation error will be given, but in many cases you will not be able to use lambda with this kind of interfaces



        The java.util.function package contains various general purpose functional interfaces such as Predicate, Consumer, Function, and Supplier.



        Also please note that you can use lambdas without this annotation.






        share|improve this answer
































          2














          You can use lambda in Java 8



           public static void main(String args) 
          tentimes(inputPrm -> System.out.println(inputPrm));
          //tentimes(System.out::println); // You can also replace lambda with static method reference


          public static void tentimes(Consumer myFunction)
          for(int i = 0; i < 10; i++)
          myFunction.accept("hello");



          For further info about Java Lambdas and FunctionalInterfaces






          share|improve this answer






























            1














            Beside other answers, I think the main reason to "why using Functional Interface other than directly with lambda expressions" can be related to nature of Java language which is Object Oriented.



            The main attributes of Lambda expressions are: 1. They can be passed around 2. and they can executed in future in specific time (several times). Now to support this feature in languages, some other languages deal simply with this matter.



            For instance in Java Script, a function (Anonymous function, or Function literals) can be addressed as a object. So, you can create them simply and also they can be assigned to a variable and so forth. For example:



            var myFunction = function (...) 
            ...;

            alert(myFunction(...));


            or via ES6, you can use an arrow function.



            const myFunction = ... => ...


            Up to now, Java language designers have not accepted to handle mentioned features via these manner (functional programming techniques). They believe that Java language is Object Oriented and therefore they should solve this problem via Object Oriented techniques. They don't want to miss simplicity and consistency of Java language.



            Therefore, they use interfaces, as when an object of an interface with just one method (I mean functional interface) is need you can replace it with a lambda expression. Such as:



            ActionListener listener = event -> ...;





            share|improve this answer






























              1














              Functional Interface:
              - Introduces from Java 8
              - Interface that contains a "single abstract" method.
              Example 1:



               interface CalcArea // --functional interface
              double calcArea(double rad);


              Example 2:
              interface CalcGeometry // --functional interface
              double calcArea(double rad);
              default double calcPeri(double rad)
              return 0.0;



              Example 3:
              interface CalcGeometry // -- not functional interface
              double calcArea(double rad);
              double calcPeri(double rad);


              - Java8 annotation -- @FunctionalInterface
              - Annotation check that interface contains only one abstract method. If not, raise error.
              - Even though @FunctionalInterface missing, it is still functional interface (if having single abstract method). The annotation helps avoid mistakes.
              - Functional interface may have additional static & default methods.
              - e.g. Iterable<>, Comparable<>, Comparator<>.


              Applications of Functional Interface
              1. Method references
              2. Lambda Expression and
              3. Constructor references


              To learn functional interface , learn first default methods in interface, and after learning functional interface it will be easy to you to understand method reference and lambda expression






              share|improve this answer

























              • Should your first two examples have 'abstract' keyword to it?

                – sofs1
                Dec 24 '18 at 10:43






              • 1





                @sofs1 Methods declared in interfaces are by default both public and abstract. You have to use abstract keyword in case of methods in abstract class. However it is fine to use abstract keyword for methods in interface also. They have permitted it for compatibility of older java version but it is discouraged.

                – Ketan
                Jan 2 at 5:15












              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36881826%2fwhat-is-use-of-functional-interface-in-java-8%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              10 Answers
              10






              active

              oldest

              votes








              10 Answers
              10






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              108














              @FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface.



              But you can use lambdas without this annotation as well as you can override methods without @Override annotation.



              From docs




              a functional interface has exactly one abstract method. Since default
              methods have an implementation, they are not abstract. If an interface
              declares an abstract method overriding one of the public methods of
              java.lang.Object, that also does not count toward the interface's
              abstract method count since any implementation of the interface will
              have an implementation from java.lang.Object or elsewhere




              This can be used in lambda expression:



              public interface Foo 
              public void doSomething();



              This cannot be used in lambda expression:



              public interface Foo 
              public void doSomething();
              public void doSomethingElse();



              But this will give compilation error:



              @FunctionalInterface
              public interface Foo
              public void doSomething();
              public void doSomethingElse();




              Invalid '@FunctionalInterface' annotation; Foo is not a functional
              interface







              share|improve this answer




















              • 35





                To be more precise, you have to have exactly one abstract method that doesn’t override a method in java.lang.Object in a functional interface.

                – Holger
                Apr 27 '16 at 8:04






              • 1





                @Holger Yeah, that's exactly what the docs says.

                – Sergii Bishyr
                Apr 27 '16 at 8:07






              • 6





                …and it’s slightly different to “not have more than one public method besides static and default”…

                – Holger
                Apr 27 '16 at 8:12







              • 2





                @Holger you are right. I have added quote from docs to my answer. Thanx! :)

                – Sergii Bishyr
                Apr 27 '16 at 8:16






              • 1





                Foo foo = o -> obj.equals(anyObj); should work.

                – lwpro2
                Jul 23 '17 at 15:25
















              108














              @FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface.



              But you can use lambdas without this annotation as well as you can override methods without @Override annotation.



              From docs




              a functional interface has exactly one abstract method. Since default
              methods have an implementation, they are not abstract. If an interface
              declares an abstract method overriding one of the public methods of
              java.lang.Object, that also does not count toward the interface's
              abstract method count since any implementation of the interface will
              have an implementation from java.lang.Object or elsewhere




              This can be used in lambda expression:



              public interface Foo 
              public void doSomething();



              This cannot be used in lambda expression:



              public interface Foo 
              public void doSomething();
              public void doSomethingElse();



              But this will give compilation error:



              @FunctionalInterface
              public interface Foo
              public void doSomething();
              public void doSomethingElse();




              Invalid '@FunctionalInterface' annotation; Foo is not a functional
              interface







              share|improve this answer




















              • 35





                To be more precise, you have to have exactly one abstract method that doesn’t override a method in java.lang.Object in a functional interface.

                – Holger
                Apr 27 '16 at 8:04






              • 1





                @Holger Yeah, that's exactly what the docs says.

                – Sergii Bishyr
                Apr 27 '16 at 8:07






              • 6





                …and it’s slightly different to “not have more than one public method besides static and default”…

                – Holger
                Apr 27 '16 at 8:12







              • 2





                @Holger you are right. I have added quote from docs to my answer. Thanx! :)

                – Sergii Bishyr
                Apr 27 '16 at 8:16






              • 1





                Foo foo = o -> obj.equals(anyObj); should work.

                – lwpro2
                Jul 23 '17 at 15:25














              108












              108








              108







              @FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface.



              But you can use lambdas without this annotation as well as you can override methods without @Override annotation.



              From docs




              a functional interface has exactly one abstract method. Since default
              methods have an implementation, they are not abstract. If an interface
              declares an abstract method overriding one of the public methods of
              java.lang.Object, that also does not count toward the interface's
              abstract method count since any implementation of the interface will
              have an implementation from java.lang.Object or elsewhere




              This can be used in lambda expression:



              public interface Foo 
              public void doSomething();



              This cannot be used in lambda expression:



              public interface Foo 
              public void doSomething();
              public void doSomethingElse();



              But this will give compilation error:



              @FunctionalInterface
              public interface Foo
              public void doSomething();
              public void doSomethingElse();




              Invalid '@FunctionalInterface' annotation; Foo is not a functional
              interface







              share|improve this answer















              @FunctionalInterface annotation is useful for compilation time checking of your code. You cannot have more than one method besides static, default and abstract methods that override methods in Object in your @FunctionalInterface or any other interface used as a functional interface.



              But you can use lambdas without this annotation as well as you can override methods without @Override annotation.



              From docs




              a functional interface has exactly one abstract method. Since default
              methods have an implementation, they are not abstract. If an interface
              declares an abstract method overriding one of the public methods of
              java.lang.Object, that also does not count toward the interface's
              abstract method count since any implementation of the interface will
              have an implementation from java.lang.Object or elsewhere




              This can be used in lambda expression:



              public interface Foo 
              public void doSomething();



              This cannot be used in lambda expression:



              public interface Foo 
              public void doSomething();
              public void doSomethingElse();



              But this will give compilation error:



              @FunctionalInterface
              public interface Foo
              public void doSomething();
              public void doSomethingElse();




              Invalid '@FunctionalInterface' annotation; Foo is not a functional
              interface








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 3 '17 at 4:20









              Andrea Bergonzo

              5491620




              5491620










              answered Apr 27 '16 at 6:30









              Sergii BishyrSergii Bishyr

              4,87041937




              4,87041937







              • 35





                To be more precise, you have to have exactly one abstract method that doesn’t override a method in java.lang.Object in a functional interface.

                – Holger
                Apr 27 '16 at 8:04






              • 1





                @Holger Yeah, that's exactly what the docs says.

                – Sergii Bishyr
                Apr 27 '16 at 8:07






              • 6





                …and it’s slightly different to “not have more than one public method besides static and default”…

                – Holger
                Apr 27 '16 at 8:12







              • 2





                @Holger you are right. I have added quote from docs to my answer. Thanx! :)

                – Sergii Bishyr
                Apr 27 '16 at 8:16






              • 1





                Foo foo = o -> obj.equals(anyObj); should work.

                – lwpro2
                Jul 23 '17 at 15:25













              • 35





                To be more precise, you have to have exactly one abstract method that doesn’t override a method in java.lang.Object in a functional interface.

                – Holger
                Apr 27 '16 at 8:04






              • 1





                @Holger Yeah, that's exactly what the docs says.

                – Sergii Bishyr
                Apr 27 '16 at 8:07






              • 6





                …and it’s slightly different to “not have more than one public method besides static and default”…

                – Holger
                Apr 27 '16 at 8:12







              • 2





                @Holger you are right. I have added quote from docs to my answer. Thanx! :)

                – Sergii Bishyr
                Apr 27 '16 at 8:16






              • 1





                Foo foo = o -> obj.equals(anyObj); should work.

                – lwpro2
                Jul 23 '17 at 15:25








              35




              35





              To be more precise, you have to have exactly one abstract method that doesn’t override a method in java.lang.Object in a functional interface.

              – Holger
              Apr 27 '16 at 8:04





              To be more precise, you have to have exactly one abstract method that doesn’t override a method in java.lang.Object in a functional interface.

              – Holger
              Apr 27 '16 at 8:04




              1




              1





              @Holger Yeah, that's exactly what the docs says.

              – Sergii Bishyr
              Apr 27 '16 at 8:07





              @Holger Yeah, that's exactly what the docs says.

              – Sergii Bishyr
              Apr 27 '16 at 8:07




              6




              6





              …and it’s slightly different to “not have more than one public method besides static and default”…

              – Holger
              Apr 27 '16 at 8:12






              …and it’s slightly different to “not have more than one public method besides static and default”…

              – Holger
              Apr 27 '16 at 8:12





              2




              2





              @Holger you are right. I have added quote from docs to my answer. Thanx! :)

              – Sergii Bishyr
              Apr 27 '16 at 8:16





              @Holger you are right. I have added quote from docs to my answer. Thanx! :)

              – Sergii Bishyr
              Apr 27 '16 at 8:16




              1




              1





              Foo foo = o -> obj.equals(anyObj); should work.

              – lwpro2
              Jul 23 '17 at 15:25






              Foo foo = o -> obj.equals(anyObj); should work.

              – lwpro2
              Jul 23 '17 at 15:25














              9














              Not at all. Lambda expressions are the one and only point of that annotation.






              share|improve this answer


















              • 6





                Well, lamdbas work without the annotation as well. It's an assertion just like @Override to let the compiler know that you intended to write something that was "functional" (and get an error if you slipped).

                – Thilo
                Apr 27 '16 at 6:34







              • 1





                Straight to the point and the correct answer, though a bit short. I took the time to add a more elaborated answer saying the same thing with more words…

                – Holger
                Apr 27 '16 at 8:41















              9














              Not at all. Lambda expressions are the one and only point of that annotation.






              share|improve this answer


















              • 6





                Well, lamdbas work without the annotation as well. It's an assertion just like @Override to let the compiler know that you intended to write something that was "functional" (and get an error if you slipped).

                – Thilo
                Apr 27 '16 at 6:34







              • 1





                Straight to the point and the correct answer, though a bit short. I took the time to add a more elaborated answer saying the same thing with more words…

                – Holger
                Apr 27 '16 at 8:41













              9












              9








              9







              Not at all. Lambda expressions are the one and only point of that annotation.






              share|improve this answer













              Not at all. Lambda expressions are the one and only point of that annotation.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 27 '16 at 6:25









              Louis WassermanLouis Wasserman

              147k20262329




              147k20262329







              • 6





                Well, lamdbas work without the annotation as well. It's an assertion just like @Override to let the compiler know that you intended to write something that was "functional" (and get an error if you slipped).

                – Thilo
                Apr 27 '16 at 6:34







              • 1





                Straight to the point and the correct answer, though a bit short. I took the time to add a more elaborated answer saying the same thing with more words…

                – Holger
                Apr 27 '16 at 8:41












              • 6





                Well, lamdbas work without the annotation as well. It's an assertion just like @Override to let the compiler know that you intended to write something that was "functional" (and get an error if you slipped).

                – Thilo
                Apr 27 '16 at 6:34







              • 1





                Straight to the point and the correct answer, though a bit short. I took the time to add a more elaborated answer saying the same thing with more words…

                – Holger
                Apr 27 '16 at 8:41







              6




              6





              Well, lamdbas work without the annotation as well. It's an assertion just like @Override to let the compiler know that you intended to write something that was "functional" (and get an error if you slipped).

              – Thilo
              Apr 27 '16 at 6:34






              Well, lamdbas work without the annotation as well. It's an assertion just like @Override to let the compiler know that you intended to write something that was "functional" (and get an error if you slipped).

              – Thilo
              Apr 27 '16 at 6:34





              1




              1





              Straight to the point and the correct answer, though a bit short. I took the time to add a more elaborated answer saying the same thing with more words…

              – Holger
              Apr 27 '16 at 8:41





              Straight to the point and the correct answer, though a bit short. I took the time to add a more elaborated answer saying the same thing with more words…

              – Holger
              Apr 27 '16 at 8:41











              9














              The documentation makes indeed a difference between the purpose




              An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.




              and the use case




              Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.




              whose wording does not preclude other use cases in general. Since the primary purpose is to indicate a functional interface, your actual question boils down to “Are there other use cases for functional interfaces other than lambda expressions and method/constructor references?”



              Since functional interface is a Java language construct defined by the Java Language Specification, only that specification can answer that question:



              JLS §9.8. Functional Interfaces:






              In addition to the usual process of creating an interface instance by declaring and instantiating a class (§15.9), instances of functional interfaces can be created with method reference expressions and lambda expressions (§15.13, §15.27).




              So the Java Language Specification doesn’t say otherwise, the only use case mentioned in that section is that of creating interface instances with method reference expressions and lambda expressions. (This includes constructor references as they are noted as one form of method reference expression in the specification).



              So in one sentence, no, there is no other use case for it in Java 8.






              share|improve this answer



























                9














                The documentation makes indeed a difference between the purpose




                An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.




                and the use case




                Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.




                whose wording does not preclude other use cases in general. Since the primary purpose is to indicate a functional interface, your actual question boils down to “Are there other use cases for functional interfaces other than lambda expressions and method/constructor references?”



                Since functional interface is a Java language construct defined by the Java Language Specification, only that specification can answer that question:



                JLS §9.8. Functional Interfaces:






                In addition to the usual process of creating an interface instance by declaring and instantiating a class (§15.9), instances of functional interfaces can be created with method reference expressions and lambda expressions (§15.13, §15.27).




                So the Java Language Specification doesn’t say otherwise, the only use case mentioned in that section is that of creating interface instances with method reference expressions and lambda expressions. (This includes constructor references as they are noted as one form of method reference expression in the specification).



                So in one sentence, no, there is no other use case for it in Java 8.






                share|improve this answer

























                  9












                  9








                  9







                  The documentation makes indeed a difference between the purpose




                  An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.




                  and the use case




                  Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.




                  whose wording does not preclude other use cases in general. Since the primary purpose is to indicate a functional interface, your actual question boils down to “Are there other use cases for functional interfaces other than lambda expressions and method/constructor references?”



                  Since functional interface is a Java language construct defined by the Java Language Specification, only that specification can answer that question:



                  JLS §9.8. Functional Interfaces:






                  In addition to the usual process of creating an interface instance by declaring and instantiating a class (§15.9), instances of functional interfaces can be created with method reference expressions and lambda expressions (§15.13, §15.27).




                  So the Java Language Specification doesn’t say otherwise, the only use case mentioned in that section is that of creating interface instances with method reference expressions and lambda expressions. (This includes constructor references as they are noted as one form of method reference expression in the specification).



                  So in one sentence, no, there is no other use case for it in Java 8.






                  share|improve this answer













                  The documentation makes indeed a difference between the purpose




                  An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.




                  and the use case




                  Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.




                  whose wording does not preclude other use cases in general. Since the primary purpose is to indicate a functional interface, your actual question boils down to “Are there other use cases for functional interfaces other than lambda expressions and method/constructor references?”



                  Since functional interface is a Java language construct defined by the Java Language Specification, only that specification can answer that question:



                  JLS §9.8. Functional Interfaces:






                  In addition to the usual process of creating an interface instance by declaring and instantiating a class (§15.9), instances of functional interfaces can be created with method reference expressions and lambda expressions (§15.13, §15.27).




                  So the Java Language Specification doesn’t say otherwise, the only use case mentioned in that section is that of creating interface instances with method reference expressions and lambda expressions. (This includes constructor references as they are noted as one form of method reference expression in the specification).



                  So in one sentence, no, there is no other use case for it in Java 8.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 27 '16 at 8:34









                  HolgerHolger

                  171k23247467




                  171k23247467





















                      6














                      As others have said, a functional interface is an interface which exposes one method. It may have more than one method, but all of the others must have a default implementation. The reason it's called a "functional interface" is because it effectively acts as a function. Since you can pass interfaces as parameters, it means that functions are now "first-class citizens" like in functional programming languages. This has many benefits, and you'll see them quite a lot when using the Stream API. Of course, lambda expressions are the main obvious use for them.






                      share|improve this answer



























                        6














                        As others have said, a functional interface is an interface which exposes one method. It may have more than one method, but all of the others must have a default implementation. The reason it's called a "functional interface" is because it effectively acts as a function. Since you can pass interfaces as parameters, it means that functions are now "first-class citizens" like in functional programming languages. This has many benefits, and you'll see them quite a lot when using the Stream API. Of course, lambda expressions are the main obvious use for them.






                        share|improve this answer

























                          6












                          6








                          6







                          As others have said, a functional interface is an interface which exposes one method. It may have more than one method, but all of the others must have a default implementation. The reason it's called a "functional interface" is because it effectively acts as a function. Since you can pass interfaces as parameters, it means that functions are now "first-class citizens" like in functional programming languages. This has many benefits, and you'll see them quite a lot when using the Stream API. Of course, lambda expressions are the main obvious use for them.






                          share|improve this answer













                          As others have said, a functional interface is an interface which exposes one method. It may have more than one method, but all of the others must have a default implementation. The reason it's called a "functional interface" is because it effectively acts as a function. Since you can pass interfaces as parameters, it means that functions are now "first-class citizens" like in functional programming languages. This has many benefits, and you'll see them quite a lot when using the Stream API. Of course, lambda expressions are the main obvious use for them.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 27 '16 at 14:39









                          Sina MadaniSina Madani

                          6451820




                          6451820





















                              5














                              A lambda expression can be assigned to a functional interface type, but so can method references, and anonymous classes.



                              One nice thing about the specific functional interfaces in java.util.function is that they can be composed to create new functions (like Function.andThen and Function.compose, Predicate.and, etc.) due to the handy default methods they contain.






                              share|improve this answer























                              • You should elaborate on this comment more. What about method references and new functions?

                                – K.Nicholas
                                Mar 14 '18 at 22:13















                              5














                              A lambda expression can be assigned to a functional interface type, but so can method references, and anonymous classes.



                              One nice thing about the specific functional interfaces in java.util.function is that they can be composed to create new functions (like Function.andThen and Function.compose, Predicate.and, etc.) due to the handy default methods they contain.






                              share|improve this answer























                              • You should elaborate on this comment more. What about method references and new functions?

                                – K.Nicholas
                                Mar 14 '18 at 22:13













                              5












                              5








                              5







                              A lambda expression can be assigned to a functional interface type, but so can method references, and anonymous classes.



                              One nice thing about the specific functional interfaces in java.util.function is that they can be composed to create new functions (like Function.andThen and Function.compose, Predicate.and, etc.) due to the handy default methods they contain.






                              share|improve this answer













                              A lambda expression can be assigned to a functional interface type, but so can method references, and anonymous classes.



                              One nice thing about the specific functional interfaces in java.util.function is that they can be composed to create new functions (like Function.andThen and Function.compose, Predicate.and, etc.) due to the handy default methods they contain.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Apr 27 '16 at 6:46









                              Hank DHank D

                              4,55221531




                              4,55221531












                              • You should elaborate on this comment more. What about method references and new functions?

                                – K.Nicholas
                                Mar 14 '18 at 22:13

















                              • You should elaborate on this comment more. What about method references and new functions?

                                – K.Nicholas
                                Mar 14 '18 at 22:13
















                              You should elaborate on this comment more. What about method references and new functions?

                              – K.Nicholas
                              Mar 14 '18 at 22:13





                              You should elaborate on this comment more. What about method references and new functions?

                              – K.Nicholas
                              Mar 14 '18 at 22:13











                              4














                              An interface with only one abstract method is called Functional Interface.
                              It is not mandatory to use @FunctionalInterface, but it’s best practice to use it with functional interfaces to avoid addition of extra methods accidentally. If the interface is annotated with @FunctionalInterface annotation and we try to have more than one abstract method, it throws compiler error.



                              package com.akhi;
                              @FunctionalInterface
                              public interface FucnctionalDemo

                              void letsDoSomething();
                              //void letsGo(); //invalid because another abstract method does not allow
                              public String toString(); // valid because toString from Object
                              public boolean equals(Object o); //valid

                              public static int sum(int a,int b) // valid because method static

                              return a+b;

                              public default int sub(int a,int b) //valid because method default

                              return a-b;







                              share|improve this answer





























                                4














                                An interface with only one abstract method is called Functional Interface.
                                It is not mandatory to use @FunctionalInterface, but it’s best practice to use it with functional interfaces to avoid addition of extra methods accidentally. If the interface is annotated with @FunctionalInterface annotation and we try to have more than one abstract method, it throws compiler error.



                                package com.akhi;
                                @FunctionalInterface
                                public interface FucnctionalDemo

                                void letsDoSomething();
                                //void letsGo(); //invalid because another abstract method does not allow
                                public String toString(); // valid because toString from Object
                                public boolean equals(Object o); //valid

                                public static int sum(int a,int b) // valid because method static

                                return a+b;

                                public default int sub(int a,int b) //valid because method default

                                return a-b;







                                share|improve this answer



























                                  4












                                  4








                                  4







                                  An interface with only one abstract method is called Functional Interface.
                                  It is not mandatory to use @FunctionalInterface, but it’s best practice to use it with functional interfaces to avoid addition of extra methods accidentally. If the interface is annotated with @FunctionalInterface annotation and we try to have more than one abstract method, it throws compiler error.



                                  package com.akhi;
                                  @FunctionalInterface
                                  public interface FucnctionalDemo

                                  void letsDoSomething();
                                  //void letsGo(); //invalid because another abstract method does not allow
                                  public String toString(); // valid because toString from Object
                                  public boolean equals(Object o); //valid

                                  public static int sum(int a,int b) // valid because method static

                                  return a+b;

                                  public default int sub(int a,int b) //valid because method default

                                  return a-b;







                                  share|improve this answer















                                  An interface with only one abstract method is called Functional Interface.
                                  It is not mandatory to use @FunctionalInterface, but it’s best practice to use it with functional interfaces to avoid addition of extra methods accidentally. If the interface is annotated with @FunctionalInterface annotation and we try to have more than one abstract method, it throws compiler error.



                                  package com.akhi;
                                  @FunctionalInterface
                                  public interface FucnctionalDemo

                                  void letsDoSomething();
                                  //void letsGo(); //invalid because another abstract method does not allow
                                  public String toString(); // valid because toString from Object
                                  public boolean equals(Object o); //valid

                                  public static int sum(int a,int b) // valid because method static

                                  return a+b;

                                  public default int sub(int a,int b) //valid because method default

                                  return a-b;








                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Oct 3 '18 at 9:47

























                                  answered Sep 26 '18 at 5:52









                                  AkhileshAkhilesh

                                  415




                                  415





















                                      2














                                      @FunctionalInterface is a new annotation are released with Java 8 and provide target types for lambda expressions and it used on compilation time checking of your code.



                                      When you want to use it :



                                      1- Your interface must not have more than one abstract methods, otherwise compilation error will be given.



                                      1- Your interface Should be pure, which means functional interface is intended to be implemented by stateless classes, exmple of pure is Comparator interface because its not depend on the implementers state, in this case No compilation error will be given, but in many cases you will not be able to use lambda with this kind of interfaces



                                      The java.util.function package contains various general purpose functional interfaces such as Predicate, Consumer, Function, and Supplier.



                                      Also please note that you can use lambdas without this annotation.






                                      share|improve this answer





























                                        2














                                        @FunctionalInterface is a new annotation are released with Java 8 and provide target types for lambda expressions and it used on compilation time checking of your code.



                                        When you want to use it :



                                        1- Your interface must not have more than one abstract methods, otherwise compilation error will be given.



                                        1- Your interface Should be pure, which means functional interface is intended to be implemented by stateless classes, exmple of pure is Comparator interface because its not depend on the implementers state, in this case No compilation error will be given, but in many cases you will not be able to use lambda with this kind of interfaces



                                        The java.util.function package contains various general purpose functional interfaces such as Predicate, Consumer, Function, and Supplier.



                                        Also please note that you can use lambdas without this annotation.






                                        share|improve this answer



























                                          2












                                          2








                                          2







                                          @FunctionalInterface is a new annotation are released with Java 8 and provide target types for lambda expressions and it used on compilation time checking of your code.



                                          When you want to use it :



                                          1- Your interface must not have more than one abstract methods, otherwise compilation error will be given.



                                          1- Your interface Should be pure, which means functional interface is intended to be implemented by stateless classes, exmple of pure is Comparator interface because its not depend on the implementers state, in this case No compilation error will be given, but in many cases you will not be able to use lambda with this kind of interfaces



                                          The java.util.function package contains various general purpose functional interfaces such as Predicate, Consumer, Function, and Supplier.



                                          Also please note that you can use lambdas without this annotation.






                                          share|improve this answer















                                          @FunctionalInterface is a new annotation are released with Java 8 and provide target types for lambda expressions and it used on compilation time checking of your code.



                                          When you want to use it :



                                          1- Your interface must not have more than one abstract methods, otherwise compilation error will be given.



                                          1- Your interface Should be pure, which means functional interface is intended to be implemented by stateless classes, exmple of pure is Comparator interface because its not depend on the implementers state, in this case No compilation error will be given, but in many cases you will not be able to use lambda with this kind of interfaces



                                          The java.util.function package contains various general purpose functional interfaces such as Predicate, Consumer, Function, and Supplier.



                                          Also please note that you can use lambdas without this annotation.







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Jul 17 '18 at 17:50

























                                          answered Jul 11 '18 at 12:13









                                          Ahmad Al-KurdiAhmad Al-Kurdi

                                          1,32721027




                                          1,32721027





















                                              2














                                              You can use lambda in Java 8



                                               public static void main(String args) 
                                              tentimes(inputPrm -> System.out.println(inputPrm));
                                              //tentimes(System.out::println); // You can also replace lambda with static method reference


                                              public static void tentimes(Consumer myFunction)
                                              for(int i = 0; i < 10; i++)
                                              myFunction.accept("hello");



                                              For further info about Java Lambdas and FunctionalInterfaces






                                              share|improve this answer



























                                                2














                                                You can use lambda in Java 8



                                                 public static void main(String args) 
                                                tentimes(inputPrm -> System.out.println(inputPrm));
                                                //tentimes(System.out::println); // You can also replace lambda with static method reference


                                                public static void tentimes(Consumer myFunction)
                                                for(int i = 0; i < 10; i++)
                                                myFunction.accept("hello");



                                                For further info about Java Lambdas and FunctionalInterfaces






                                                share|improve this answer

























                                                  2












                                                  2








                                                  2







                                                  You can use lambda in Java 8



                                                   public static void main(String args) 
                                                  tentimes(inputPrm -> System.out.println(inputPrm));
                                                  //tentimes(System.out::println); // You can also replace lambda with static method reference


                                                  public static void tentimes(Consumer myFunction)
                                                  for(int i = 0; i < 10; i++)
                                                  myFunction.accept("hello");



                                                  For further info about Java Lambdas and FunctionalInterfaces






                                                  share|improve this answer













                                                  You can use lambda in Java 8



                                                   public static void main(String args) 
                                                  tentimes(inputPrm -> System.out.println(inputPrm));
                                                  //tentimes(System.out::println); // You can also replace lambda with static method reference


                                                  public static void tentimes(Consumer myFunction)
                                                  for(int i = 0; i < 10; i++)
                                                  myFunction.accept("hello");



                                                  For further info about Java Lambdas and FunctionalInterfaces







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Sep 17 '18 at 6:06









                                                  Akiner AlkanAkiner Alkan

                                                  1,685426




                                                  1,685426





















                                                      1














                                                      Beside other answers, I think the main reason to "why using Functional Interface other than directly with lambda expressions" can be related to nature of Java language which is Object Oriented.



                                                      The main attributes of Lambda expressions are: 1. They can be passed around 2. and they can executed in future in specific time (several times). Now to support this feature in languages, some other languages deal simply with this matter.



                                                      For instance in Java Script, a function (Anonymous function, or Function literals) can be addressed as a object. So, you can create them simply and also they can be assigned to a variable and so forth. For example:



                                                      var myFunction = function (...) 
                                                      ...;

                                                      alert(myFunction(...));


                                                      or via ES6, you can use an arrow function.



                                                      const myFunction = ... => ...


                                                      Up to now, Java language designers have not accepted to handle mentioned features via these manner (functional programming techniques). They believe that Java language is Object Oriented and therefore they should solve this problem via Object Oriented techniques. They don't want to miss simplicity and consistency of Java language.



                                                      Therefore, they use interfaces, as when an object of an interface with just one method (I mean functional interface) is need you can replace it with a lambda expression. Such as:



                                                      ActionListener listener = event -> ...;





                                                      share|improve this answer



























                                                        1














                                                        Beside other answers, I think the main reason to "why using Functional Interface other than directly with lambda expressions" can be related to nature of Java language which is Object Oriented.



                                                        The main attributes of Lambda expressions are: 1. They can be passed around 2. and they can executed in future in specific time (several times). Now to support this feature in languages, some other languages deal simply with this matter.



                                                        For instance in Java Script, a function (Anonymous function, or Function literals) can be addressed as a object. So, you can create them simply and also they can be assigned to a variable and so forth. For example:



                                                        var myFunction = function (...) 
                                                        ...;

                                                        alert(myFunction(...));


                                                        or via ES6, you can use an arrow function.



                                                        const myFunction = ... => ...


                                                        Up to now, Java language designers have not accepted to handle mentioned features via these manner (functional programming techniques). They believe that Java language is Object Oriented and therefore they should solve this problem via Object Oriented techniques. They don't want to miss simplicity and consistency of Java language.



                                                        Therefore, they use interfaces, as when an object of an interface with just one method (I mean functional interface) is need you can replace it with a lambda expression. Such as:



                                                        ActionListener listener = event -> ...;





                                                        share|improve this answer

























                                                          1












                                                          1








                                                          1







                                                          Beside other answers, I think the main reason to "why using Functional Interface other than directly with lambda expressions" can be related to nature of Java language which is Object Oriented.



                                                          The main attributes of Lambda expressions are: 1. They can be passed around 2. and they can executed in future in specific time (several times). Now to support this feature in languages, some other languages deal simply with this matter.



                                                          For instance in Java Script, a function (Anonymous function, or Function literals) can be addressed as a object. So, you can create them simply and also they can be assigned to a variable and so forth. For example:



                                                          var myFunction = function (...) 
                                                          ...;

                                                          alert(myFunction(...));


                                                          or via ES6, you can use an arrow function.



                                                          const myFunction = ... => ...


                                                          Up to now, Java language designers have not accepted to handle mentioned features via these manner (functional programming techniques). They believe that Java language is Object Oriented and therefore they should solve this problem via Object Oriented techniques. They don't want to miss simplicity and consistency of Java language.



                                                          Therefore, they use interfaces, as when an object of an interface with just one method (I mean functional interface) is need you can replace it with a lambda expression. Such as:



                                                          ActionListener listener = event -> ...;





                                                          share|improve this answer













                                                          Beside other answers, I think the main reason to "why using Functional Interface other than directly with lambda expressions" can be related to nature of Java language which is Object Oriented.



                                                          The main attributes of Lambda expressions are: 1. They can be passed around 2. and they can executed in future in specific time (several times). Now to support this feature in languages, some other languages deal simply with this matter.



                                                          For instance in Java Script, a function (Anonymous function, or Function literals) can be addressed as a object. So, you can create them simply and also they can be assigned to a variable and so forth. For example:



                                                          var myFunction = function (...) 
                                                          ...;

                                                          alert(myFunction(...));


                                                          or via ES6, you can use an arrow function.



                                                          const myFunction = ... => ...


                                                          Up to now, Java language designers have not accepted to handle mentioned features via these manner (functional programming techniques). They believe that Java language is Object Oriented and therefore they should solve this problem via Object Oriented techniques. They don't want to miss simplicity and consistency of Java language.



                                                          Therefore, they use interfaces, as when an object of an interface with just one method (I mean functional interface) is need you can replace it with a lambda expression. Such as:



                                                          ActionListener listener = event -> ...;






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Sep 14 '18 at 13:44









                                                          MMKaramiMMKarami

                                                          34427




                                                          34427





















                                                              1














                                                              Functional Interface:
                                                              - Introduces from Java 8
                                                              - Interface that contains a "single abstract" method.
                                                              Example 1:



                                                               interface CalcArea // --functional interface
                                                              double calcArea(double rad);


                                                              Example 2:
                                                              interface CalcGeometry // --functional interface
                                                              double calcArea(double rad);
                                                              default double calcPeri(double rad)
                                                              return 0.0;



                                                              Example 3:
                                                              interface CalcGeometry // -- not functional interface
                                                              double calcArea(double rad);
                                                              double calcPeri(double rad);


                                                              - Java8 annotation -- @FunctionalInterface
                                                              - Annotation check that interface contains only one abstract method. If not, raise error.
                                                              - Even though @FunctionalInterface missing, it is still functional interface (if having single abstract method). The annotation helps avoid mistakes.
                                                              - Functional interface may have additional static & default methods.
                                                              - e.g. Iterable<>, Comparable<>, Comparator<>.


                                                              Applications of Functional Interface
                                                              1. Method references
                                                              2. Lambda Expression and
                                                              3. Constructor references


                                                              To learn functional interface , learn first default methods in interface, and after learning functional interface it will be easy to you to understand method reference and lambda expression






                                                              share|improve this answer

























                                                              • Should your first two examples have 'abstract' keyword to it?

                                                                – sofs1
                                                                Dec 24 '18 at 10:43






                                                              • 1





                                                                @sofs1 Methods declared in interfaces are by default both public and abstract. You have to use abstract keyword in case of methods in abstract class. However it is fine to use abstract keyword for methods in interface also. They have permitted it for compatibility of older java version but it is discouraged.

                                                                – Ketan
                                                                Jan 2 at 5:15
















                                                              1














                                                              Functional Interface:
                                                              - Introduces from Java 8
                                                              - Interface that contains a "single abstract" method.
                                                              Example 1:



                                                               interface CalcArea // --functional interface
                                                              double calcArea(double rad);


                                                              Example 2:
                                                              interface CalcGeometry // --functional interface
                                                              double calcArea(double rad);
                                                              default double calcPeri(double rad)
                                                              return 0.0;



                                                              Example 3:
                                                              interface CalcGeometry // -- not functional interface
                                                              double calcArea(double rad);
                                                              double calcPeri(double rad);


                                                              - Java8 annotation -- @FunctionalInterface
                                                              - Annotation check that interface contains only one abstract method. If not, raise error.
                                                              - Even though @FunctionalInterface missing, it is still functional interface (if having single abstract method). The annotation helps avoid mistakes.
                                                              - Functional interface may have additional static & default methods.
                                                              - e.g. Iterable<>, Comparable<>, Comparator<>.


                                                              Applications of Functional Interface
                                                              1. Method references
                                                              2. Lambda Expression and
                                                              3. Constructor references


                                                              To learn functional interface , learn first default methods in interface, and after learning functional interface it will be easy to you to understand method reference and lambda expression






                                                              share|improve this answer

























                                                              • Should your first two examples have 'abstract' keyword to it?

                                                                – sofs1
                                                                Dec 24 '18 at 10:43






                                                              • 1





                                                                @sofs1 Methods declared in interfaces are by default both public and abstract. You have to use abstract keyword in case of methods in abstract class. However it is fine to use abstract keyword for methods in interface also. They have permitted it for compatibility of older java version but it is discouraged.

                                                                – Ketan
                                                                Jan 2 at 5:15














                                                              1












                                                              1








                                                              1







                                                              Functional Interface:
                                                              - Introduces from Java 8
                                                              - Interface that contains a "single abstract" method.
                                                              Example 1:



                                                               interface CalcArea // --functional interface
                                                              double calcArea(double rad);


                                                              Example 2:
                                                              interface CalcGeometry // --functional interface
                                                              double calcArea(double rad);
                                                              default double calcPeri(double rad)
                                                              return 0.0;



                                                              Example 3:
                                                              interface CalcGeometry // -- not functional interface
                                                              double calcArea(double rad);
                                                              double calcPeri(double rad);


                                                              - Java8 annotation -- @FunctionalInterface
                                                              - Annotation check that interface contains only one abstract method. If not, raise error.
                                                              - Even though @FunctionalInterface missing, it is still functional interface (if having single abstract method). The annotation helps avoid mistakes.
                                                              - Functional interface may have additional static & default methods.
                                                              - e.g. Iterable<>, Comparable<>, Comparator<>.


                                                              Applications of Functional Interface
                                                              1. Method references
                                                              2. Lambda Expression and
                                                              3. Constructor references


                                                              To learn functional interface , learn first default methods in interface, and after learning functional interface it will be easy to you to understand method reference and lambda expression






                                                              share|improve this answer















                                                              Functional Interface:
                                                              - Introduces from Java 8
                                                              - Interface that contains a "single abstract" method.
                                                              Example 1:



                                                               interface CalcArea // --functional interface
                                                              double calcArea(double rad);


                                                              Example 2:
                                                              interface CalcGeometry // --functional interface
                                                              double calcArea(double rad);
                                                              default double calcPeri(double rad)
                                                              return 0.0;



                                                              Example 3:
                                                              interface CalcGeometry // -- not functional interface
                                                              double calcArea(double rad);
                                                              double calcPeri(double rad);


                                                              - Java8 annotation -- @FunctionalInterface
                                                              - Annotation check that interface contains only one abstract method. If not, raise error.
                                                              - Even though @FunctionalInterface missing, it is still functional interface (if having single abstract method). The annotation helps avoid mistakes.
                                                              - Functional interface may have additional static & default methods.
                                                              - e.g. Iterable<>, Comparable<>, Comparator<>.


                                                              Applications of Functional Interface
                                                              1. Method references
                                                              2. Lambda Expression and
                                                              3. Constructor references


                                                              To learn functional interface , learn first default methods in interface, and after learning functional interface it will be easy to you to understand method reference and lambda expression







                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Jan 27 at 0:35









                                                              sofs1

                                                              92652246




                                                              92652246










                                                              answered Dec 18 '18 at 12:53









                                                              KetanKetan

                                                              166




                                                              166












                                                              • Should your first two examples have 'abstract' keyword to it?

                                                                – sofs1
                                                                Dec 24 '18 at 10:43






                                                              • 1





                                                                @sofs1 Methods declared in interfaces are by default both public and abstract. You have to use abstract keyword in case of methods in abstract class. However it is fine to use abstract keyword for methods in interface also. They have permitted it for compatibility of older java version but it is discouraged.

                                                                – Ketan
                                                                Jan 2 at 5:15


















                                                              • Should your first two examples have 'abstract' keyword to it?

                                                                – sofs1
                                                                Dec 24 '18 at 10:43






                                                              • 1





                                                                @sofs1 Methods declared in interfaces are by default both public and abstract. You have to use abstract keyword in case of methods in abstract class. However it is fine to use abstract keyword for methods in interface also. They have permitted it for compatibility of older java version but it is discouraged.

                                                                – Ketan
                                                                Jan 2 at 5:15

















                                                              Should your first two examples have 'abstract' keyword to it?

                                                              – sofs1
                                                              Dec 24 '18 at 10:43





                                                              Should your first two examples have 'abstract' keyword to it?

                                                              – sofs1
                                                              Dec 24 '18 at 10:43




                                                              1




                                                              1





                                                              @sofs1 Methods declared in interfaces are by default both public and abstract. You have to use abstract keyword in case of methods in abstract class. However it is fine to use abstract keyword for methods in interface also. They have permitted it for compatibility of older java version but it is discouraged.

                                                              – Ketan
                                                              Jan 2 at 5:15






                                                              @sofs1 Methods declared in interfaces are by default both public and abstract. You have to use abstract keyword in case of methods in abstract class. However it is fine to use abstract keyword for methods in interface also. They have permitted it for compatibility of older java version but it is discouraged.

                                                              – Ketan
                                                              Jan 2 at 5:15


















                                                              draft saved

                                                              draft discarded
















































                                                              Thanks for contributing an answer to Stack Overflow!


                                                              • Please be sure to answer the question. Provide details and share your research!

                                                              But avoid


                                                              • Asking for help, clarification, or responding to other answers.

                                                              • Making statements based on opinion; back them up with references or personal experience.

                                                              To learn more, see our tips on writing great answers.




                                                              draft saved


                                                              draft discarded














                                                              StackExchange.ready(
                                                              function ()
                                                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36881826%2fwhat-is-use-of-functional-interface-in-java-8%23new-answer', 'question_page');

                                                              );

                                                              Post as a guest















                                                              Required, but never shown





















































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown

































                                                              Required, but never shown














                                                              Required, but never shown












                                                              Required, but never shown







                                                              Required, but never shown







                                                              Popular posts from this blog

                                                              Use pre created SQLite database for Android project in kotlin

                                                              Darth Vader #20

                                                              Ondo