ANOTHER WAY OF WORKING WITH INTERFACE IN JAVAsteemCreated with Sketch.

in java •  4 years ago 

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.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!