Java Functional Interface

Latest posts
Get notified of the Latest Sport News Update from Our Blog
Share

It’s an interface with just only one abstract method. it can have any number of default or static methods . lambdas can only operate on functional interface. it is also referred as Single Abstract Method Interfaces, or SAM Interfaces

interface Foo1{
    void bar();
}

interface Foo2 {
    int bar(boolean isFoo);
}

interface Foo3{
    String bar (Object obj , int i1);
}
interface Foo4 {
    default String bar(){
        return "baz" ;  //it's default so not counted
    }
    void mee();

}

when you declare functional interface @FunctionalInterface can be added , but if you applied on that interface which is not functional compiler error will be generated

@FunctionalInterface
interface Foo5{
    void bar(); 
}

@FunctionalInterface
interface BlankFoo1 extends Foo3{
    //it  inherits abstract method from Foo3

}

@FunctionalInterface
interface Foo6{
    void bar();
    @Override
    boolean equals(Object obj); // override one of objects method so not counted 

}

// this is not functional interface 
interface BadFoo {
    void bar();
    void quux(); // <-- Second method prevents lambda: 
   }

// this is not functional interface , doesn't have any methods 
interface BlankInterface { }

Leave a Reply

Your email address will not be published. Required fields are marked *

Related blogs

Nullam quis risus eget urna mollis ornare vel eu leo. Aenean lacinia bibendum nulla sed 

Subscribe to get 15% discount