Last Updated : 02 Aug, 2025
In Java, a marker Interface is an empty interface that has no fields or methods. It is used just to mark or tag a class to tell Java or other programs something special about that class. These interfaces do not have any methods inside but act as metadata to provide information about the class. Examples of marker interfaces include Serializable, Cloneable and Remote.
Working of the Marker InterfaceNote: When a class implements a marker interface, it indicates to the system that it supports certain operations or features associated with that interface.
A marker interface is a special type of interface because it does not have any methods, so when a class implements a marker interface, it means it is giving a signal to Java that this class should be treated differently. Let's understand this with the help of an example. Suppose that when a class implements the Serializable interface, it is telling Java that objects of this class can be turned into a stream of bytes.
Example:
Java
//Driver Code Starts
interface Serializable {
// Marker Interface
}
//Driver Code Ends
// Define Person class that implements the marker interface
class Person implements Serializable {
public Person() {
System.out.println("Person object created");
}
}
// Define Animal class does not implement marker interface
class Animal {
public Animal() {
System.out.println("Animal object created");
}
}
public class Geeks
{
public static void main(String[] args)
{
Person person = new Person();
//Driver Code Starts
Animal animal = new Animal();
if (person instanceof Serializable)
System.out.println("Person is serializable");
else
System.out.println("Person is not serializable");
if (animal instanceof Serializable)
System.out.println("Animal is serializable");
else
System.out.println("Animal is not serializable");
}
}
//Driver Code Ends
Person object created Animal object created Person is serializable Animal is not serializableReal-time Example of Marker Interface
There are some more real time examples of Marker Interface which are used in real-time applications. Like Cloneable Interface, Serializable interface, etc. Let us check one by one:
1. Cloneable Interface
Cloneable interface is present in java.lang package. There is a method clone() in Object class. A class that implements the Cloneable interface indicates that it is legal for clone() method to make a field-for-field copy of instances of that class. Invoking Object's clone method on an instance of the class that does not implement the Cloneable interface results in an exception CloneNotSupportedException being thrown. By convention, classes that implement this interface should override Object.clone() method.
Example:
Java
import java.lang.Cloneable;
// Making class A cloneable using cloneable interface
class A implements Cloneable
{
int i;
// A class constructor
public A(int i) {
this.i = i;
}
// Overriding clone() method by simply calling Object class clone() method.
@Override
protected Object clone()
throws CloneNotSupportedException
{
return super.clone();
}
}
public class Geeks
{
public static void main(String[] args)
throws CloneNotSupportedException
{
A a = new A(20);
// cloning 'a' and holding new cloned object reference in b
// down-casting as clone() return type is Object
A b = (A)a.clone();
System.out.println(b.i);
}
}
2. Serializable Interface
Serializable interface is present in java.io package. It is used to make an object eligible for saving its state into a file. This is called Serialization. Classes that do not implement serializable will not have their state serialized or deserialized. All subtypes of a serializable class are themselves serializable.
Example:
Java
import java.io.*;
class A implements Serializable
{
int i;
String s;
// A class constructor
public A(int i,String s)
{
this.i = i;
this.s = s;
}
}
public class Geeks
{
public static void main(String[] args)
throws IOException, ClassNotFoundException
{
A a = new A(20,"GeeksForGeeks");
// Serializing 'a'
FileOutputStream fos = new FileOutputStream("xyz.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(a);
// De-serializing 'a'
FileInputStream fis = new FileInputStream("xyz.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
A b = (A)ois.readObject(); // down-casting object
System.out.println(b.i+" "+b.s);
// closing streams
oos.close();
ois.close();
}
}
3. Remote Interface
Remote interface is present in java.rmi package. A remote object is an object which is stored at one machine and accessed from another machine. So, to make an object a remote object, we need to flag it with Remote interface. Here, Remote interface serves to identify interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface. RMI (Remote Method Invocation) provides some convenience classes that remote object implementations can extend which facilitate remote object creation.
Note: Marker interfaces were once used to tag classes without adding methods. Since Java 5, annotations (like @Override or @Deprecated) are preferred they're clearer, more flexible and can hold extra data. While marker interfaces still exist for backward compatibility, annotations are the modern choice for adding metadata.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4