Code Pumpkin

Marker Interface Interview Questions

December 12, 2017
Posted by Abhi Andhariya

The interface in java is a mechanism to achieve abstraction and it is one of the most discussed topic in entry and mid level java developer interviews. In our previous articles, we have seen various interface related topics like Interface Evolution in JavaDifference between interface and abstract class and Functional interface in java 8.

In this post, we are going to discuss about another special type of interfaces i.e. Marker interface. Here is the list of topics which we will cover in this article.

  1. What is Marker Interface in Java?
  2. What Is The Use Of Marker Interface?
  3. What Are Built In Marker Interfaces?
  4. Can We Create Our Own Marker Interface? If Yes, How?
  5. What Are The Problems With Marker Interfaces?
  6. Can't We Achieve The Same Behaviour With Annotations?

Marker Interface interview Questions

What is Marker interface in Java?

Marker interface is the interface which doesn't contain any field or method. It is just an empty interface.

What is the use of marker interface?

If interface doesn't have any methods or field, then what is the use of it?

Well. its name itself suggests that it is used for marking something, isn't it? It is used to convey to the JVM or compiler that all the classes, which implements particular marker interface, have some specific behaviour.

They are also known as tag interfaces since they tag all the derived classes into a category based on their purpose.

For example,

All the class which implements Serializable interface are eligible for Serialization process. If we try to Serialize an object of a class which has not implemented Serializable interface, then it will throw NotSerializableException.

In other words, just by marking a class as Serializable, we can make it eligible for Serialization. Amazing, isn't it?

Marker interfaces are just an empty interfaces used to mark or identify a special operation. For example, Cloneable interface is used to mark cloning operation and Serializableinterface is used to mark serialization and deserialization of an object.

What are built in marker interfaces?

There are four main in-built marker interfaces in jdk. Here is the quick summary of them.

  1. Serializable interface

Serializability of a class is enabled by the class implementing the interface.

  1. Cloneable interface

Object’s clone() method may be called. Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

  1. RandomAccess interface

It is used by List implementations to indicate that they support fast (generally constant time) random access.

  1. Remote interface

It serves to identify interfaces whose methods may be invoked from a non-local virtual machine.

Can we create our Own Marker Interface? If yes, how?

Yes, We can create our own marker interface just by creating a blank interface. In our code, we can use it as in method arguments or use it with instanceof operator to execute code based on type of object. For Example,

public interface MyMarkerInterface {}

public class MyMarkedClass implements MyMarkerInterface {}

Then you can for example have method taking only MyMarkerInterface instance:

public myMethod(MyMarkerInterface x) {}

or check with instanceof at runtime like 

if (obj instanceof MyMarkerInterface ) {
    // do something

What are the Problems with marker interfaces?

One of the common problem with marker interface is that when a class implements them, all of its subclass by default inherits it. For Example,

class A implements Serializable{}

class B extends A {}

In above case, class B inheritably behaves as Serializable. We can not remove or unimplement Serializable from it. 

We need to handle such exceptional cases by throwing exceptions from child classes.

can't We achieve the same behaviour with Annotations?

Most of the thing that can be done with marker interfaces can be done with annotations. Annotations can have parameters of various kinds, and they're much more flexible. However there are some drawbacks of Annotations as well, For example,

  1. To check if object is an instance of an interface at runtime, one can use instanceof which is a relatively low-cost operation. Using annotations just to check type of the object at run time is very high as it requires Java reflection calls which is far more costly.
  2. Marker interfaces are better than annotations when they're used to define a type. For example, Serializable can be used (and should be used) as the type of an argument that must be serializable. An annotation doesn't allow doing that:

    public void writeToFile(Serializable object);

Immutable Class from Effective Java

Refer Item 37: Use marker interfaces to define types of Joshua Bloch's Effective Java to know more use cases and differences between Marker interface and marker annotations.

That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning 🙂

If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.

We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.

About the Author

Surviving Java Developer, Passionate Blogger, Table Tennis Lover, Bookworm, Occasional illustrator and a big fan of Joey Tribbiani, The Walking Dead and Game of Thrones...!!

Tags: , , ,

Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
For more information on supported HTML tags in disqus comment, click here.
Total Posts : 124
follow us in feedly

Like Us On Facebook