Functional Interface Java

By | | Updated : 2021-03-12 | Viewed : 995 times

Functional Interface Java

In this tutorial, we will try to understand the Functional Interfaces. What is a Functional Interface and will learn about the default method and static method in the Functional interface?. And we will focus on functional interfaces in java 8 with examples.

What is Functional Interface in Java 8?

Functional Interface is the first step towards achieving Functional Programming with lambda expressions. A Functional Interface is an interface with one abstract method. So we can pass the lambda expression to that abstract method as implementation. In each time of passing of the lambda expression to that abstract method is a different one.

Default methods in Functional Interface

The interface contains any number of abstract methods but in Functional Interface should contain only one abstract method and any number of default methods.

We will write with the full definition of Functional Interface how it will be like and which contains the default methods.

Vehicle.java
@FunctionalInterface
interface Vehicle {
    	public default void getNoOfWheels {        
            System.out.println("The Vehicle will have four wheel");
        }
    	public default void getNoOfStreeingWheels {        
            System.out.println("The Vehicle will have One Steering wheel");
        }
 	public String getVehicleBrand();
}

Notice the Functional Interface defined above contains two default methods. The concept of the default method, introduced in Java 8, will be used to overcome the problem of interfaces. The problem is When a new method is required in the implemented classes of the interface then the new method has to add in the remaining implemented classes.

The concept of the default method was introduced to overcome the above-given problem. When one newly added method which is the default method is not required to implement in all the derived classes. So it will be easy to add the new method to add in the functional interface. Wherever it is required to add we can add that method implementation.

Static methods in Functional Interface

Default methods in a Functional interface work like a function in the same way the static method works. But the difference is that the static methods in the interface can be overridden but static methods in a functional interface can not be overridden.

Please notice the code snippet of static methods in Functional Interface.

Vehicle.java
@FunctionalInterface
interface Vehicle {
	//static methods
	public static void displayVehicle{
		System.out.println("Vehicle");
	}

	//default methods
        public default void getNoOfWheels {        
            System.out.println("The Vehicle will have four wheels");
        }
        public default void getNoOfStreeingWheels {        
            System.out.println("The Vehicle will have One Steering wheel");
        }
	
	//abstract methods
    	public String getVehicleBrand();
}
Leave A Reply