Functional interface in java 8

In Java 8, the concept of functional interfaces was introduced. A functional interface is an interface that has only one abstract method. It is used to enable the functional programming paradigm in Java, allowing the use of lambda expressions or method references to represent a single unit of behavior.

Here are some key points about functional interfaces in Java 8:

  1. Single Abstract Method (SAM) Interface: A functional interface is also known as a Single Abstract Method (SAM) interface because it contains only one abstract method. It may also contain default methods or static methods, but those do not affect its status as a functional interface.
  2. @FunctionalInterface Annotation: To explicitly declare an interface as a functional interface, you can use the @FunctionalInterface annotation. This annotation is optional but provides compile-time safety by ensuring that the interface has only one abstract method. If more than one abstract method is defined, the compiler will report an error.
  3. Lambda Expressions and Method References: Functional interfaces are designed to be used with lambda expressions and method references. Lambda expressions provide a concise way to express behavior, and functional interfaces act as the target types for lambdas. You can use lambda expressions or method references to instantiate functional interfaces.
  4. Built-in Functional Interfaces: Java 8 introduced several built-in functional interfaces in the java.util.function package to cover common use cases. These interfaces include Predicate, Function, Consumer, Supplier, and more. Each interface represents a specific type of operation or transformation and comes with its corresponding functional method(s).
  5. Custom Functional Interfaces: You can create your own functional interfaces by defining an interface with a single abstract method. This allows you to define behavior-specific interfaces tailored to your application’s requirements.

Functional interfaces and lambda expressions greatly simplify the implementation of functional programming concepts in Java, making code more concise, readable, and expressive. They enable the use of higher-order functions and functional constructs in Java, promoting a more functional programming style.

Here’s an example of a functional interface in Java 8:

In this example, we define a functional interface called Calculator. It has a single abstract method named calculate that takes two integers as parameters and returns an integer result.

Now, we can use lambda expressions or method references to instantiate the Calculator interface and provide the implementation for the calculate method.

Here are a couple of examples:

In this example, we create two instances of the Calculator interface: addition and subtraction. The first one uses a lambda expression to define the addition operation, while the second one uses a method reference to refer to the subtract method defined in the Main class.

We can then invoke the calculate method on these instances, passing the required parameters to perform the respective calculations. The output demonstrates the results of the addition and subtraction operations.

This example showcases the usage of a functional interface and how lambda expressions or method references can be used to provide the implementation for its single abstract method.

About Manohar

I, Manohar am the founder and chief editor of I am working in an IT professional.

View all posts by Manohar →

Leave a Reply