Is lesson me hum seekhenge:
- Functional Interface kya hota hai
- Lambda ke saath relation
- @FunctionalInterface annotation
- Built-in Functional Interfaces
- Real examples
Functional Interface ka matlab:
aise interface jisme sirf ek abstract method hota hai
interface MyInterface {
void show();
}✔ sirf ek abstract method → valid functional interface
Functional interface ko use karte hain:
lambda expression ke saath
Example:
MyInterface obj = () -> {
System.out.println("Hello");
};
obj.show();@FunctionalInterface
interface MyInterface {
void show();
}✔ compile-time check karta hai
✔ ensure karta hai sirf 1 abstract method ho
interface Test {
void m1();
void m2(); // error
}interface Test {
void show(); // abstract
default void display(){
System.out.println("Default method");
}
static void print(){
System.out.println("Static method");
}
}✔ allowed hai
✔ count nahi hote abstract methods me
Java me already ready interfaces hote hain:
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println(isEven.test(4));Function<Integer, Integer> square = x -> x * x;
System.out.println(square.apply(5));Consumer<String> print = s -> System.out.println(s);
print.accept("Hello");Supplier<Integer> random = () -> 100;
System.out.println(random.get());@FunctionalInterface
interface Calculator {
int add(int a, int b);
}Use:
Calculator c = (a, b) -> a + b;
System.out.println(c.add(10, 20));Button click listener
event handling
✔ sirf ek abstract method hona chahiye
✔ lambda ke saath use hota hai
✔ default & static allowed
✔ code short hota hai
✔ readable hota hai
✔ functional programming support
- Functional interface kya hota hai?
- @FunctionalInterface annotation ka use kya hai?
- kya multiple methods allowed hain?
- lambda ka relation kya hai?
Is lesson me humne seekha:
✔ Functional Interface concept
✔ Lambda relation
✔ Built-in interfaces
✔ Custom examples
Functional Interface Java me lambda expressions ka backbone hota hai.