@FunctionalInterface
(optional but recommended) to indicate that it must have only one abstract method.
default
and static
methods, but only one abstract method.
java.util.function
package):
Runnable
β No argument, no return value (used in multithreading).Callable<V>
β No argument, returns a value (used in multithreading).Predicate<T>
β Takes one argument and returns boolean
.Function<T,R>
β Takes one argument and returns a value.Consumer<T>
β Takes one argument and returns nothing (void).Supplier<T>
β Takes no argument and returns a value.@FunctionalInterface
interface MyInterface
{
// Single abstract method (SAM)
void m1();
// You can still have default or static methods
default void m2()
{
System.out.println("Default method inside functional interface");
}
}
@FunctionalInterface
interface MyInterface
{
void greet(String name);
}
class MyClass implements MyInterface
{
@Override
public void greet(String name)
{
System.out.println("Hello, " + name + "!");
}
}
public class MainApp3
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.greet("Deepak"); // Calls the implemented method
}
}
Hello, Deepak!
@FunctionalInterface
interface MyInterface
{
void greet(String name);
}
public class MainApp3
{
public static void main(String[] args)
{
MyInterface mi = new MyInterface() {
@Override
public void greet(String name) {
System.out.println("Hello, " + name + "!");
}
};
mi.greet("Deepak");
}
}
Hello, Deepak!
class MainApp3$1 implements MyInterface
{
@Override
public void greet(String name)
{
System.out.println("Hello, " + name + "!");
}
}
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.