Difference between interface and functional interface in Java with example

Difference between interface and functional interface in Java with example

Difference between interface and functional interface in Java with example

This is a quick java refresher to understand the difference between interface and functional interface along with review of a few other related core java interview questions asked frequently.

Introduction

In its most common form, an interface is a group of related methods with empty bodies.

interface Bike{
       void changeGear(int newGear);
       void speedUp(int newSpeed);
       void applyBrakes(int decrement);
}
  • Implementing an interface allows a class to become more formal about its behavior.
  • Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.
  • If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
  • A functional interface is just a special type of interface which implements a single method.

Note – As part of this article we’ll cover few additional related questions that could be asked in Core java interview.

  • What is difference between interface and functional interface?
  • Which interface is a functional interface?
  • What is a marker interface for?

What is functional interface?

Functional interfaces are created in Java to have single functionality which is defined in the abstract method inside them. A lambda expression is generally used as instance to trigger the behavior of the functional interfaces. An important point to note here is that the functional interface can have zero or more static methods or default methods but they must have one and only one abstract method.

For example java.lang.Runnable is a functional interface having a single method run() to implement.

public interface Runnable{
      void run();
}
private void doSomething() {
     // Old way,
     new Thread(new Runnable() {

	@Override
	public void run() {
		System.out.println("Running as anonymous, old style!");
	}
     }).start();
// ----------------------------------------------------
     // New functional interface
     new Thread(() -> {
	System.out.println("Running as lambda using functional interface!");
     }).start();
}

Output is –

Running as anonymous, old style!
Running as a lambda using functional interface!

Java has may other internal functional interfaces as well:

  • java.util.Comparator
  • java.lang.Runnable
  • java.io.FileFilter
  • java.util.concurrent.Callable

Difference Between Interface and Functional Interface

A functional interface is an interface that contains only a single abstract method. They abstract a single task to be done. These interfaces in Java have been introduced after the release of Java 8.

In Java, an interface is a reference type that specifies a set of methods that a class must implement. One key difference between an interface and a functional interface is that a class implementing a normal interface can have multiple methods which are defined in that interface. On the other hand, a functional interface can only have one abstract method, which means that a class implementing a functional interface can only have one abstract method.

Another difference between an interface and a functional interface is that a functional interface can be used as the target type of a lambda expression or method reference. This means that you can use a functional interface as a shorthand way of specifying a single method interface in your code. For example, you could use a functional interface to specify a comparator or a runnable.

While, a functional interface has exactly one abstract method. Functional interface, like any other interface, can also have one or more static or default method in interface, but there can be only one abstract method and lambda is defined against this method.

InterfaceFunctional Interface
0 or more methods insideMust have one abstract method. Can have one or more default/static methods.
Can’t be used as target type of a lambda expression if more than one methods definedcan be used as the target type of a lambda expression or method reference
for example interface Bike as defined aboveFor example Runnable – having run() method
Been with Java since beginningIntroduced in Java since Java 8
Difference between interface and functional interface

Marker Interface in Java

Adding this as side note to revise – Marker Interface in Java is an interface that doesn’t have any method or constant inside it. They are used to tag the group of classes with some meta information that JVM can later use to process in some ‘special’ way. I’ve explained these details in earlier marker interface in java example programs that we created before.

What is Marker Interface used for?

We already covered this question – marker interfaces are empty interface used to tag or group specific type of classes so that JVM can identify and apply some logic on them. Go through this marker interface in java example for more clarity.

What is difference between interface and functional interface and marker interface?

As mentioned above,

  • Functional Interfaces have one abstract method defined in them so that Lambda could be called against them.
  • Normal interfaces in general don’t have this restriction. They can have one or more abstract methods.
  • Marker Interface do not have any method or constants defined in them.

Summary

To summarise the difference between interface and functional interface is the number of abstract methods that they can contain. A functional interface is just a special type of interface which may have a single abstract method/task to be implemented.

Hope you like this article. As always please drop a comment in case of any queries and we’ll try to revert as soon as possible.

Further Reading

Want to learn more about JAVA interfaces and core concepts in OOPs? Refer to below articles and start preparing for your core java interview right away:

Please follow and like us:
Pin Share

Leave a Reply

Your email address will not be published. Required fields are marked *