What is Functional Interface in java | Full Stack With Java

Functional Interface in java: – A Functional interface Can Consist of Exactly One Incomplete Method in it.

Table of Contents

Functional Interface in java

Example 1

@FunctionalInterface
public interface A {      // Correct
public void test();
}

Example 2

@FunctionalInterface
public interface A {      // Error
public void test1();
public void test2();
}

Note – Functional Interface Was Introduced in Version 8 of java.

  • Functional Interfaces Can Be Implemented by a Lambda Expression.
  • We Cannot do Multiple inheritances on the Functional Interface.
  • We Can Create Any No. of Complete Methods inside Functional Interface.

Default keyword in java

  1. Using the default keyword we create a complete method in an interface.
  2. this feature was introduced in version 8 of java.
public interface A{ 
public void test1();
default void test2() {
	System.out.println(100);
}
}
public class B implements A{
	@Override
	public void test1() {
		System.out.println(200);
	}
	public static void main(String[] args) {
		B b1 = new B();
		b1.test1();
		b1.test2();
	}
}
Output: 
200
100

You Also Learn –

Note 1 – We can create any number of complete methods inside the functional interface.

@FunctionalInterface
public interface A{ 
public void test1();
default void test2(){
	System.out.println(200);
}
default void test3() {
	System.out.println(300);
}

}
public class B implements A{
	@Override
	public void test1() {
		System.out.println(100);
	}
	public static void main(String[] args) {
		B b1 = new B();
		b1.test1();
		b1.test2();
		b1.test3();
	}
}
Output: 
100
200
300

Note 2 – We can not do multiple inheritances on the functional interface.

Leave a Comment