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 } 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"); } } 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); // } }

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 }
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");
}
}
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);
// }
}