ANOTHER WAY OF WORKING WITH INTERFACE IN JAVA

Words
62
Reading
1 min
Listen
Play
6y

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.

ANOTHER WAY OF WORKING WITH INTERFACE IN JAVA | Ecency