userimage

Interface and Functional interface in java

In java an interface in java is a blueprint of a class having static constants or abstract methods only. The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java.


public interface BluePrintOfClass {

        String name;

        void className();

}


And functional interface is an interface which is having only one method in it and the interface annotated with @FunctionalInterface


@FunctionalInterface

public interface FuncInterface { 

         public void doStuff();

}


From java 8 a new concept of default method implementation in interfaces. It"s added in java 8 for giving backward compatibility so that old interfaces can be used to leverage the lambda expression capability of Java 8. Let say we want add a new method in existing "List" or "Collection" interfaces and we do not want to break existing implemented classes so we can add "newMethod" method declaration. So in java 8 default method introduced so that List/Collection interface can have a default implementation of "newMethod" method, and the class implementing these interfaces need not implement the same.


public interface TestCollection {

   default void printTest(){

      System.out.println("Print test collection!");

   }

}