Marker interface in Java is an empty interface having no fields or methods. Simply we can say that it’s an interface which is used to convey the JVM that a class executing an interface of this category will have some special behavior.
Need for Marker Interface
1. Marker interface in java is used to point out to JVM that the class implementing marker interface will have some special characteristics.
2. Marker interface also endow a way to associate metadata with the class where the language support is not existing.
3. Marker interface is used by many languages to provide run time type information about the object.
Example of some marker interfaces are given below.
1. Serializable Interface
Serialization is the process of writing the state of object to a byte stream. This is useful when you want to save the state of your program to a persistent storage area, such as file. At a later time, you may restore these object by using the process of deserialization.
Serialization is also needed to implement Remote Method Invocation (RMI). RMI allows a java object on one machine to invoke a method of a java object on a different machine. An object may be supplied as an argument to that remote method. The sending machine serializes the object and transmits it and the receiving machine deserializes it. The whole procedure of serialization and deserialization is free from JVM interference.
2. Cloneable Interface
Cloneable interface is used to indicate that a class allows a bitwise copy of an object to be made. It allows a developer to create the clone of an object. When a clone is made, the constructor for the object being cloned is not called. A clone is simply an exact copy of the original. But there is one restriction that the object of which the clone is created, must implement the cloneable interface.
There is a method clone() in object class. Cloneable interface implemented by class indicates that it is legal for clone() method to make a copy object of that class. As clonning cause unintended side effects hence it is a potentially dangerous action, therefore clone( ) is declared as protected inside object. This means that it must be explicitly overridden by that class so that it is public or it must either be called from within a method defined by the class that implements Cloneable.
3. Remote Interface
A Remote interface is an interface that declares a set of methods that may be invoked from a remote Java virtual machine. Remote interface is present in java.rmi package. So to make an object a remote object, we need to banner it with Remote interface Here, Remote interface serves to distinguish interfaces whose techniques might be conjured from a non-neighborhood virtual machine. Any object that is a remote object must at least extend, either directly or indirectly, the interface java.rmi.Remote.
An RMI server program require to create the initial remote object and then export them to the RMI runtime, which makes them available for receiving incoming remote invocations.
4. ThreadSafe Interface
A marker interface called ThreadSafe can be utilized to communicate different developers that classes actualizing this marker interface gives Thread safe guarantee and any change ought not violate that.
Now for better understanding here an example for serialization:
package com; import java.io.*; class NewString implements Serializable { String data; public NewString(String instring) { data=instring; } public String toString() { return data; } } public class SerializationDemo { public static void main (String[] args) { NewString inobject, outobject; outobject=new NewString("hello from java"); try { FileOutputStream fileoutputstring= new FileOutputStream("serialized.dat"); ObjectOutputStream objectoutputstream= new ObjectOutputStream(fileoutputstring); objectoutputstream.writeObject(outobject); objectoutputstream.flush(); objectoutputstream.close(); FileInputStream fileinputstring= new FileInputStream("serialized.dat"); ObjectInputStream objectinputstream= new ObjectInputStream(fileinputstring); inobject=(NewString)objectinputstream.readObject(); objectinputstream.close(); System.out.println(inobject); } catch(Exception e) { e.printStackTrace(); } } }
Output
hello from java
Comment below if you found any information incorrect or have doubts related to above tutorial about marker interface in Java.
Marker Interface is used to convey to the JVM that the class implementing an interface of this category will have some special behavior. Many thanks for sharing this tutorial.