In Java, you can create a functional interface using the @FunctionalInterface annotation or by defining an interface with a single abstract method. A functional interface is an interface that contains only one abstract method and is used to represent a single functionality or behavior.
Here’s an example of creating a functional interface in Java:
<span class="hljs-meta">@FunctionalInterface</span>
<span class="hljs-keyword">interface</span> <span class="hljs-title class_">MyFunctionalInterface</span> {
    <span class="hljs-keyword">void</span> <span class="hljs-title function_">doSomething</span><span class="hljs-params">()</span>;
}
In the example above, MyFunctionalInterface is a functional interface with a single abstract method doSomething(). The @FunctionalInterface annotation is optional but provides additional safety by ensuring that the interface does not contain more than one abstract method.
Once you have defined the functional interface, you can use it to create lambda expressions or method references to implement the abstract method. Here’s an example of using the functional interface:
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Main</span> {
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
        <span class="hljs-type">MyFunctionalInterface</span> <span class="hljs-variable">functionalObj</span> <span class="hljs-operator">=</span> () -> {
            <span class="hljs-comment">// Implementation of the abstract method</span>
            System.out.println(<span class="hljs-string">"Doing something..."</span>);
        };
        functionalObj.doSomething(); <span class="hljs-comment">// Invoke the abstract method using the lambda expression</span>
    }
}
In the example above, we create an instance of the functional interface MyFunctionalInterface using a lambda expression and provide the implementation for the doSomething() method. Finally, we invoke the doSomething() method using the functional interface object.
Functional interfaces are commonly used with lambda expressions or method references to provide a concise way of representing behavior that can be passed around as an argument or assigned to variables.
