For the post on interface, please click here. There are many ways to work with interface in java basically to remove the unnecessary code. Here's a program to showing two of these.
package Polymorphism;
interface A{
void output();
}
public class InterfaceClassLambda {
public static void main(String[] args) {
A obj=new A(){
public void output() {
System.out.println("Hello World");
}
};
obj.output();
}
}
Or you can work with interface through a mechanism known as Lambda Expression, where you can create function without requiring a class.
interface A{
void output();
}
public class InterfaceClassLambda {
public static void main(String[] args) {
A obj= () ->
System.out.println("Hello World");
obj.output();
}
}
Both will show the same output when executed.