Marker Interface Vs Functional Interface difference

Marker Interface Vs Functional Interface difference

Marker Interface Vs Functional Interface

This is a quick java refresher to understand the difference between Marker Interface vs Functional Interface, along with review of a few other related core java interview questions asked frequently.

Introduction

Functional interfaces in Java have been introduced after the release of Java 8. In comparison to this, Marker Interface in Java have been part of the language since the start. They are used to tag the group of classes with some meta information that JVM can later use to process these classes in some ‘special’ way. I’ve explained these details in earlier marker interface in java example programs that we created before. Since Java 5 marker interface has been made a little obsolete after the introduction of Annotations which can also ‘stamp’ a class with some kind of meta info.

This question was asked to me long time back in a technical interview, when I was trying as a Java Fresher. There are few more java interview questions which infrequently occur in core java interview rounds and we’ll try to cover them as well.

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

Difference Between Marker Interface Vs Functional Interface

Frankly speaking, there is a very simple and obvious difference between these two. Marker interface as the name suggests just mark the class/objects and they do not have any method or property in them. 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.

Marker InterfaceFunctional Interface
No methods insideMust have one abstract method. Can have one or more default/static methods.
Built-in marker interface in java like Cloneable, Serializable – VM takes care of this behavior without anyone overriding clone or serialize behaviorsFor example Runnable – having run() method
Been with Java since beginningIntroduced in Java since Java 8
Marker Interface Vs Functional Interface difference

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.

Java has may internal functional interfaces for example :

  • java.util.Comparator
  • java.lang.Runnable
  • java.io.FileFilter
  • java.util.concurrent.Callable
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 a lambda using functional interface!");
		}).start();
}

Output is –

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

What is marker interface 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?

As mentioned above, functional interfaces have one abstract method defined in them so that Lambda could be called against them, while interfaces in general don’t have this restriction. They can have one or more abstract methods or even zero in case of marker interface.

Summary

Functional and Marker, both are special type of interfaces for java and both have their own use cases for them. While marker is used to group set of objects, functional interface works as a task for lambda. I hope this article gave you some details about both the interfaces and also clarified the difference between Marker Interface Vs Functional Interface.

In case of any questions, feel free to drop a comment 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 –

Leave a Reply

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