Part 1 : Functional Interface in Java 8

Functional Interface
What is the use of Functional Interface in Java 8?
When to use Functional Interface in Java 8?

An interface having only single abstract method is called as functional interface. Functional interface can have multiple default or static methods.

We can create a functional interface as below :

package com.onlyfullstack.lambdaAndFunctional;

public interface IConnection {

 public void connect();
 
 default void print() {
  System.out.println("in default method");
 }
 
 static void description() {
  System.out.println("in static method");
 }
}


Functional interface can have only one abstract method(method without body) and can have many default and static methods. As of now we don’t have any restriction to add more abstract methods in above interface but it will break the functional interface rule.

 

@FunctionalInterface

This is an Interface level annotation type used on functional interface to restrict the user from adding more than one abstract method.
If we try to add two or more abstract methods in @FunctionalInterface then it will give us a compilation error as below :

Invalid ‘@FunctionalInterface’ annotation; IConnection is not a functional interfacefunctional1

What is the need of static method in Interface ?

Java 8 has added static methods in interface to provide utility methods on interface level without creating the object of the interface.
Some of the static methods of the java 8 are as below :
Stream.of()
Stream.iterate()
Stream.empty()

 

What is the need of having a default method in an interface ?

We can provide some default functionality with default methods.
The original motivation to introduce default methods to Java 8 was the desire to extend the Collections Framework interfaces with lambda-oriented methods without breaking any existing implementations. Although this is more relevant to the authors of public libraries.

Java has to implement forEach functionality on Collections but we cant not add a new abstract method in each interface and add the implementation of them into their respective implementation classes. This approach will break the existing code and force everyone to give the implementation of that new method.

What if I say that the implementation of forEach method is common for all List ? Still do we need to define the same method in all classes(ArrayList or LinkedList) ?

The answer is No. For example The forEach isn’t declared by java.util.List nor the java.util.Collection interface yet. One obvious solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. However, once published, it is impossible to add methods to an interface without breaking the existing implementation.

The benefit that default methods bring is that now it’s possible to add a new default method to the interface and it doesn’t break the old implementations. So that’s how java has added lots of default methods without breaking the implementation and provided the backward compatibility.

Some of the default methods available in Map interface :
default V putIfAbsent(K key, V value)
default boolean remove(Object key, Object value)


Source Code
Download source code of Java 8 features from below git repository :
java-8-features

Java Features Tutorial

https://www.onlyfullstack.com/java-8-features-tutorial/ 


Lets go to our next tutorial where we will discuss below points :

– What is Lambda Expression in Java 8?
– Lambda Expression with Parameters