Search This Blog

Total Pageviews

Java Functional Interfaces

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.

public interface SnowCleaningInterface{
  public void cleanSnow();

// Object Class methods added to the interface.

public interface SnowCleaningInterface{
  public void cleanSnow();
  public String toString();
  public boolean equals(Object o);

//One interface can have any no of default method implementations

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 () {
      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){

Clean Snow- Used Simple Impl.
Clean Snow - Used Lambda exp.

No comments:

MongoDB Tutorial MongoDB Tutorial Table of Contents Introduction to MongoDB Installation Creating a Database Creati...