Functional Interfaces:
-------------------------------
- Single Abstract Method Interfaces.
- Create Anonymous Inner classes using interfaces.
- Use @FunctionalInterface annotation.
- Object class abstract methods can be declared inside Functional Interface.
@FunctionalInterface
public interface SnowCleaningInterface{
public void cleanSnow();
}
// Object Class methods added to the interface.
@FunctionalInterface
public interface SnowCleaningInterface{
public void cleanSnow();
public String toString();
public boolean equals(Object o);
}
//One interface can have any no of default method implementations
@FunctionalInterface
public interface CarSnowCleaningInterfaceextends SnowCleaningInterface{
default public void cleanSnowOnCar(){
System.out.println("Clean Snow on Car");
}
public void cleanSnow() ;
}
/*
* interface by creating an
* anonymous inner class vs
* lambda expression.
*/
public class SnowCleaningTest{
public static void main(String[] args) {
canCleanSnow(new SnowCleaningInterface () {
@Override
public void cleanSnow() {
System.out.println("Clean Snow- Used Simple Impl");
}
});
canCleanSnow(() -> System.out.println("Clean Snow - Used Lambda exp"));
}
public static void canCleanSnow(SnowCleaningInterface snowcleaning){
snowcleaning.cleanSnow();
}
}
//Output:
Clean Snow- Used Simple Impl.
Clean Snow - Used Lambda exp.
No comments:
Post a Comment