Marker interface having no methods right.. then what is the use of marker interface? for what purpose we use marker interface?

One of the "clean" features of the Java programming language is that it mandates a separation between interfaces (pure behavior) and classes (state and behavior). Interfaces are used in Java to specify the behavior of derived classes.
Often you will come across interfaces in Java that have no behavior. In other words, they are just empty interface definitions. These are known as marker interfaces. Some examples of marker interfaces in the Java API include:


- java,lang.Cloneable
- java,io.Serializable
- java.util.EventListener

Marker interfaces are also called "tag" interfaces since they tag all the derived classes into a category based on their purpose. For example, all classes that implement the Cloneable interface can be cloned (i.e., the clone() method can be called on them). The Java compiler checks to make sure that if the clone() method is called on a class and the class implements the Cloneable interface. For example, consider the following call to the clone() method on an object o:

SomeObject o = new SomeObject();
SomeObject ref = (SomeObject)(o.clone());

If the class SomeObject does not implement the interface Cloneable (and Cloneable is not implemented by any of the superclasses that SomeObject inherits from), the compiler will mark this line as an error. This is because the clone() method may only be called by objects of type "Cloneable." Hence, even though Cloneable is an empty interface, it serves an important purpose.

Questions by khalid_it

Showing Answers 1 - 13 of 13 Answers

Kiran Moogi

  • Dec 15th, 2006
 

We have clone() method in the Object class.If we want to create a copy of any object(to clone an object), in that case the corresponding class should implements the Cloneable interface, so we are marking that class (It can be cloneable) that it can be cloned.For this purpose we have marker interfaces in  java.

  Was this answer useful?  Yes

ray

  • Aug 16th, 2007
 

When you say serializable, cloneable JVM understands that this thing is to be serialized or cloned it does not need any methods again in this.

  Was this answer useful?  Yes

kavya

  • Mar 28th, 2018
 

It is a kind of interface which has no method is known as marker interface. Serializable, Clonnable is the example of marker interface to create Marker Interface in Java

Use of Marker Interface:
Marker Interfaces are used to indicate something to compiler/JVM. If JVM see that a class is a object of Marker Interface then it will perform some special operation.

Take an example with Serializable, Clonnable marker interface, if JVM see a Class is Serialized/Clonnable then It will do some special operation on it, similar way if JVM sees one Class is implemented custom marker interface which is created by ourself then the JVM do some special operation.

  Was this answer useful?  Yes

syam pujala

  • Mar 28th, 2018
 

Purpose of marker interface is to separate object from normal objects. ex: if you want to serialize one object then that object must implement serializable interface which is marker interface.

  Was this answer useful?  Yes

kavya

  • May 9th, 2018
 

Example of Marker Interface in Java
We have created the below three java files to make you understand.
1. MyMarkerInterface.java (which is own created marker interface)
2. ImplMarkerInterface.java (A java class which implements my marker interface)
3. FunctionalityMarkerInterface.java (which help you to understand how the JVM do special operation for marker interface)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions