Create a Java program to demonstrate method overriding with a subclass that overrides a method from an abstract class and uses a lambda expression to implement the method
The use of a lambda expression within a method in the context of method overriding in a subclass. Here's an explanation of the code:
In the main method:
public class MethodOverridingWithLambdaDemo { public static void main(String[] args) { Subclass subObj = new Subclass(); MyFunctionalInterface myLambda = () -> { System.out.println("Lambda expression called!"); }; subObj.doSomething(myLambda); } } @FunctionalInterface interface MyFunctionalInterface { void myMethod(); } abstract class AbstractClass { abstract void doSomething(MyFunctionalInterface func); } class Subclass extends AbstractClass { @Override void doSomething(MyFunctionalInterface func) { System.out.println("Inside Subclass"); func.myMethod(); } }
Inside Subclass Lambda expression called!
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions