Java 8 Interface with Static and Default methods




In java 8 we can define default methods with implementation details, which can be called using that interface reference.
Also you can define a static method in the interface which can be used as a utility method.
In case if multiple interface have same kind default method available and a class implements that multiple interface then it will have a compile time error.
But if the class provide its own implementation for that default method then it wont show compile time error.

interface Bird{
public void displayMessage();
static void printName(String name){
System.out.println("New name is: "+name);
}
default void defaultName(){
System.out.println("Sparrow is Default Name");
}
}

public class DefaultInterfaceFlight {
public static void main(String[] args){
Bird b = new Bird(){
@Override
public void displayMessage() {
System.out.println("Hello Interface");
}};
b.displayMessage();
b.defaultName();
Bird.printName("Dove");
}
}

Output
Hello Interface
Sparrow is Default Name
New name is: Dove