Java – Functional Interface

Functional Interface:

  • Interface that accepts only one abstract method.
  • We must define with annotation @FunctionalInterface.
  • Functional Interface allow static and default methods.
@FunctionalInterface
interface Test
{
            void m1();
            void m2();  // Error :
}

Static Methods and Default Methods in Functional Interface:

@FunctionalInterface
interface First
{
            static void m1(){
                        System.out.println(“Static method”);
            }
            default void m2(){
                        System.out.println(“Default method”);
            }
            void m3();
}
class Second implements First
{
            public void m3(){
                        System.out.println(“Instance method”);
            }
}
class Main
{
            public static void main(String[] args)
            {
                        First obj = new Second();
                        First.m1();
                        obj.m2();
                        obj.m3();
            }
}
Scroll to Top