0% found this document useful (0 votes)
47 views7 pages

Java Functional Interface Overview

A functional interface is an interface that contains only one abstract method. It can also contain default and static methods. The @FunctionalInterface annotation can be used to explicitly mark an interface as a functional interface. Functional interfaces allow instances to be created using lambda expressions, method references, or constructor references. Some examples of functional interfaces in Java include interfaces in the java.util.function package as well as the Runnable interface.

Uploaded by

Shubham Uke
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views7 pages

Java Functional Interface Overview

A functional interface is an interface that contains only one abstract method. It can also contain default and static methods. The @FunctionalInterface annotation can be used to explicitly mark an interface as a functional interface. Functional interfaces allow instances to be created using lambda expressions, method references, or constructor references. Some examples of functional interfaces in Java include interfaces in the java.util.function package as well as the Runnable interface.

Uploaded by

Shubham Uke
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CJC By Kunal Sir [FUNCTIONAL INTERFACE]

Functional interface

1)Interface which have only one abstract method that is call functional interface

2) Functional interface is also call Single abstract method interface(SAM interface)

3)@FunctionalInterface Annotation provide in jdk 8 inside [Link] packagen

4)if any interface annoted with @FunctionInterface then that interface cannot be more then one
abstract method if we try to write then it will give compile time error

5)if we do not mark interface with @FunctionalInterface and that interface have only one abstract
method then that interface is also call functional interface

6)inside functional interface we can write any number of default and static method

7) If functional interface declares an abstract method overriding one of the public methods of Java
Object class, that will also not be counted.

8) The instances of functional interface can be created by using lambda expressions, method
references, or constructor references.

9)A new package [Link] has been added with bunch of functional interfaces for lamda
Expression and method reference

10)if interface extends functional interface and doesn't declare any abstract method then the new
interface is also call functional interface

Example-1

package [Link];

@FunctionalInterface

public interface I {

void m1();

}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]

package [Link];

public class A implements I{

@Override

public void m1() {

[Link]("m1---A");

package [Link];

public class Test {

public static void main(String[] args) {

I i=new A();

i.m1();

Example 2

by using Anonymous class

package [Link];

@FunctionalInterface

public interface I {

void m1();

}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]

package [Link];

public class Test {

public static void main(String[] args) {

I i=new I(){

public void m1()

[Link]("m1---Annonumius");

};

i.m1();

Example-3

by using Lambda Expression

package [Link];

@FunctionalInterface

public interface I {

void m1();

}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]

package [Link];

public class Test {

public static void main(String[] args) {

I i=()->{[Link]("m1---Lambda"); };

i.m1();

Example -4

Runnable Interface

package [Link];

public class MyRunnable implements Runnable{

@Override
public void run() {
[Link]("run--method of
MyRunnable");

}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]

package [Link];
public class Test {

public static void main(String[] args) {

MyRunnable mr=new MyRunnable();


Thread t=new Thread(mr);
[Link]();

}
------------------------------
Example-5
By using Anonymus class
package [Link];
public class Test {

public static void main(String[] args) {

Runnable r=new Runnable() {

@Override
public void run() {
[Link]("run--
anonymus class");

}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]

};

Thread t=new Thread(r);


[Link]();

Example -6

By using Lambda Expresion

package [Link];
public class Test {

public static void main(String[] args) {

Runnable r=()-> [Link]("run-


-Lambda Expression");

Thread t=new Thread(r);


[Link]();

}
CJC By Kunal Sir [FUNCTIONAL INTERFACE]

Common questions

Powered by AI

In a Java project, using a functional interface like @FunctionalInterface ensures that developers adhere to a specific design pattern emphasizing a single responsibility and clarity in the interface's purpose. It prevents accidental introduction of multiple abstract methods and encourages the use of lambda expressions for more concise and maintainable code .

When a functional interface declares an abstract method overriding a public method from the Object class, it does not count towards the single abstract method requirement of functional interfaces .

Lambda expressions and method references facilitate the use of functional interfaces by allowing you to create instances of the interface without needing a separate implementing class. This is efficient for inline implementation of the single abstract method .

When implementing a functional interface using a named class, you define a class separately that implements the interface and overrides its abstract method. With an anonymous class, you inline the class definition while creating an instance, providing the implementation of the abstract method directly. Both provide an instance that executes the interface's method .

The @FunctionalInterface annotation in Java ensures that the interface adheres to the contract of having only one abstract method. If an interface is incorrectly annotated with multiple abstract methods, it generates a compile-time error .

A new interface can still be considered a functional interface if it extends another functional interface and doesn't declare any new abstract methods. It inherits the single abstract method from the parent interface, thus keeping the functional interface contract intact .

Default and static methods can be added to functional interfaces without affecting their status as functional interfaces. These methods provide additional functionality without introducing new abstract methods, thereby adhering to the single abstract method rule .

Lambda expressions simplify code by reducing boilerplate syntax and improving readability when implementing functional interfaces like Runnable. However, they can obscure understanding if overused or misused, especially for those unfamiliar with concise and functional style programming .

The Runnable interface is considered a functional interface because it has only one abstract method, run(). It can be instantiated using a traditional implementing class, an anonymous class, or a lambda expression .

A functional interface in Java is an interface that contains exactly one abstract method, making it suitable for lambda expression and method reference usage. This is also referred to as a Single Abstract Method (SAM) interface because it mandates just one unimplemented method .

You might also like