Functional Interfaces (Before Java 8)

Functional Programming With Streams Tutorial

Functional Interfaces Before Java 8

  1. Runnable
  2. Callable
  3. ActionListener
  4. Comparable
  5. Comparator

There were plenty of interfaces with a single abstract method even before Java 8.

The term functional interface was introduced in Java 8.

Runnable and Callable interfaces are commonly used in multithreaded applications.

ActionListener interface is commonly used in Swing framework based applications when making GUIs.

Comparable and Comparator interfaces are commonly used when sorting objects.

Among all these interfaces mentioned above, the Comparator interface is the only pure functional interface. This means that it's intended to be implemented by stateless objects.

#Runnable Functional Interface

This interface has a single abstract method called run().

This method takes no arguments and no return values. i.e

	
public interface Runnable {

    void run();
}		

This interface is commonly used to represent the body of a Thread. i.e

	
public Thread( Runnable runnable)	

So basically, what we do, is that we usually pass in an object that implements the Runnable interface to this constructor of the Thread class.

A new Thread will start and this new Thread will start its execution from the run() method of the Runnable object.

Therefore the run() method of the Runnable object acts as the entry point of a new Thread.

#Callable Functional Interface

This parameterized interface is commonly used to represent a Thread returning a value of type  V  . i.e

	
public interface Callable<V> {

    V call();
}		

The Callable<V> interface has a call() abstract method that returns an object of type  V  .

Since a Thread of this kind returns a value, It can't be used directly with a Thread class, but we can use it with something called an ExecutorService interface.

The ExecutorService interface has an abstract method called Future<?> submit(Runnable task) which accepts a runnable object.

The submit() method will return a Future<V> representing pending completion of the task.

#ActionListener Functional Interface

The ActionListener interface has an abstract method called void actionPerformed(ActionEvent e) that accepts an object of type ActionEvent. i.e

	
public interface ActionListener {

    void actionPerformed(ActionEvent e);
}		

The meaning of this interface is to provide callback functions for GUI events.

#Comparable Functional Interface

The Comparable<T> interface has an abstract method called int compareTo(T o) that accepts an object of type  T  as an argument and compares it to another object and the comparison returns an integer. i.e

	
public interface Comparable {

    int compareTo(T o);
}		

Such an interface can be used to sort a Collection.

NOTE: Here the elements of the Collection need to implement Comparable.

Here, an object is "comparable" if it can compare itself with another object of the same type.

#Comparator Functional Interface

The Comparator<T> interface has an abstract method called int compare(T o1, T o2) that accepts 2 objects of type  T  as arguments and compares them and the comparision returns an integer. i.e

	
public interface Comparator<T> {

    int compare​(T o1, T o2);
}		

Such an interface can be used to sort a Collection.

NOTE: This time, the elements of the Collection do not need to implement any particular interface. This is so, because we're separately providing an ordering criterion.

This has been a brief introduction of the pre-Java-8 functional interfaces.

We'll dig deeper into each of these interfaces as latest articles become live here.

Make sure you sign up for the newsletter so that you never miss an article.

Sharing is caring. Share this article to help out others getting started with functional interfaces.

Merci!

About The Author   

Steven Mwesigwa

Software Engineer at Vogue Book Consultancy Services Ltd


Picture Of Steven Mwesigwa

Steve is currently a software developer at Vogue Book Consultancy Services Ltd and a technology author. He holds a Dip. in civil engineering from Kyambogo University. He founded...