SEARCH BY

All

  • All
  • Java8
  • Spring

Functional Interfaces in java8

Post a Comment
Functional interface has only one abstract method which are also called as Single Abstract Method Interfaces (SAM interfaces).
@FunctionalInterface annotation helps to create functional interface
@FunctionalInterface
public interface SampleFunctionalInterface{
public void methodName();
// Create another method will give you an error because we declared this interface as Functional interface  by annotating with FunctionalInterface
}

We can add default methods other than one abstract method because we have implementation for default methods.
@FunctionalInterface
public interface FuntionalInterfaceExample{

public void display(); // this is an abstract method

// we can add default methods , which is valid
// because default methods has implementation

default void method1(){
//Method body
}

default String method2()
{

// method body

}
}

We can override java.lang.Object class methods in Functional interface.

Comparator is a functional interface but it has two methods compare() and equals() , but here equals() is the method which will be overriden 
jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox