Marker Interface in Java and Use


Marker interface is main frequently asked question to JAVA interview. Generally interviewer asked this question to check internal know knowledge of JAVA developer some times ask on architect level also because it’s follow the Marker Interface Design pattern. Here I have covered everything to crack questions related to Marker Interface.

What is a Marker Interface?

Marker Interfaces are empty interface it does not contains any properties and behaviors to implement. It’s also called as marker or the tag interface.

Why Marker Interface and Who Implement it?

Marker interface functionality is predefined implicitly in JVM. When a class implement a marker interface then class is not expected to implement anything to adhere to the contract defined by the interface. In contrast it is a tag to indicate the JVM regarding an expected functionality to perform implicitly.

For Example :

java.io.Serializable : If class implements the Serializable interface then JVM perform some special operation on it and writes the state of the object into object stream then this object stream will available to read by another JVM.

java.lang.Cloneable: Same way if class is implementing Cloneable interface then it perform some special operation to clone object by copy all fields

What are available Marker Interfaces in JAVA?

Mainly used built-in marker interfaces are as below :

  • java.lang.Cloneable
  • java.io.Serializable
  • java.rmi.Remote
  • java.util.EventListener (its officially know as ‘tagging interface’)

There are some more marker interface present in Java.

  • java.util.concurrent.CompletableFuture.AsynchronousCompletionTask
  • java.sql.ParameterMetaData
  • javax.xml.stream.events.EndDocument
  • javax.management.loading.PrivateClassLoader
  • java.security.KeyStore.Entry
  • java.security.KeyStore.LoadStoreParameter
  • java.security.KeyStore.ProtectionParameter
  • java.security.Policy.Parameters
  • javax.security.auth.callback.Callback
  • javax.security.auth.login.Configuration.Parameter

Can we create custom marker interfaces ?

Yes

How to Create Custom marker Interfaces?

Custom Marker Interface is nothing to do specially with JVM end . It’s to mentioned in class methods to treat this object specially so that perform some special operations.

Steps to create marker interface :

  • Create an interface with no properties and method on it.
  • Implements this interface on class.
  • Perform any operation in method if object instance of type marker interface.

See below example for more detail

Create Empty  Interface

public interface MyMarker {
//no properties and method
}

Write a class to implement interface

public class MyClass implements MyMarker{
//define properties and method
}

Class to check Marker Interface to perform special operation

public class TestMarker {

	public static void main(String[] args) {
		MyClass myClass=new MyClass();

		if(myClass instanceof MyMarker)
		{
			System.out.println("I am special treat me VIP");
		}

	}

}

Output :

I am special treat me VIP

Summary

  • Explained about Marker Interface and how JVM handle Marker Interface.
  • Available Marker Interface in Java with some example.
  • Creation of Custom Marker Interface and use.
Advertisements

About Saurabh Gupta

My Name is Saurabh Gupta, I have approx. 10 Year of experience in Information Technology World manly in Java/J2EE. During this time I have worked with multiple organization with different client, so many technology, frameworks etc.
This entry was posted in Example, Inreface, Java and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

One Response to Marker Interface in Java and Use

  1. Pingback: java.lang.CloneNotSupportedException [solved] | Facing Issues On IT

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s