ANOTHER WAY OF WORKING WITH INTERFACE IN JAVA

in Education5 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.

Sort:  

Thanks for sharing an other Java tutorial. I would request you to share your previous link in new post as it will be helpful for java enthusiast to see your other amazing tutorial.

Your post has been submitted to be curated with @gitplait community account because this is the kind of publications we like to see in our community.

Join our Community on Hive