DEV Community

Cover image for java-8 Features,Functional Interface,Lambda Expressions
Neelakandan R
Neelakandan R

Posted on

3 1 1 1 1

java-8 Features,Functional Interface,Lambda Expressions

What is a Functional Interface?

A Functional Interface is a special kind of interface in Java that has only one abstract method (a method without a body).

A Functional Interface is an interface in Java that has only one abstract method.
It is used with lambda expressions to write clean and short code.

It can have other methods that have a body (these are called default or static methods).

But it must have exactly one method that needs to be implemented.

Single Abstract Method

It has only one abstract method.

This is the main feature that makes it "functional".

Supports Lambda Expressions

Functional interfaces can be used with lambda expressions to write shorter, cleaner code.

Can Have Default and Static Methods

Besides the one abstract method, it can have default or static methods with a body.

Marked with @FunctionalInterface (Optional)

You can use the @FunctionalInterface annotation to tell the compiler to check that the interface has only one abstract method.

What is a Lambda Expression?

A lambda expression is a short way to write a method.
It is used with functional interfaces (which have only one method).

Syntax of Lambda Expressions

(argument list) -> { body of the expression }
Enter fullscreen mode Exit fullscreen mode

Image description

1. Print something:

() -> System.out.println("Hello!")

2. Square a number:

x -> x * x

3. Add two numbers:

(a, b) -> a + b

package demo;

@FunctionalInterface
public interface Interface_Demo {

    public int add(int no1, int no2);
//  public int subtract(int no1, int no2);

    public static void main(String[] args) {
        Interface_Demo.display();
    }
    public static void display()
    {
        //utility functions
        System.out.println("Hello");
    }

    public default void test()
    {
        System.out.println("Hi");
    }
}
Enter fullscreen mode Exit fullscreen mode
package demo;

public class Demo{// implements Interface_Demo{

    public static void main(String[] args) {
        //Demo demo = new Demo(); 
        //demo.add(100,200);
        Interface_Demo.display();



Interface_Demo id =  (no1, no2) -> no1+no2; 
        int result = id.add(123, 321);
        System.out.println(result);

    }

//  @Override
//  public void add(int no1, int no2) {
//      // TODO Auto-generated method stub
//      System.out.println(no1+no2); 
//  }

}


Enter fullscreen mode Exit fullscreen mode

Runner H image

An AI Agent That Handles Life, Not Just Work

From ordering flowers to booking your dinner — let Runner H turn your ideas into actions. No prompts, no hassle. Just outcomes.

Try for Free

Top comments (1)

Collapse
 
deividas_strole profile image
Deividas Strole

Great informative article! Thanks for sharing!

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay