Last Updated: 28 May, 2022
An interface that does not contain any property (fields and methods) is known as Marker Interface in Java. It is also known as a Tagging Interface.
The Marker interface provides the run-time type information about an object, so the compiler and JVM have additional information about the object.
Example of Marker Interface
Built-in Marker interface means those interfaces that are already present in the JDK with an empty body and ready to use in Java Programming.
Java has many built-in Marker Interfaces, such as java.io.Serializable, java.lang.Cloneable, java.rmi.Remote and java.util.RandomAccess etc.
All these built-in Marker Interfaces do not have any method and field. They only add special behavior to the classes implementing them.
java.io.Serializable : In order to achieve Serialization and DeSerialization in Java we must implement the java.io.Serializable interface with class.
java.lang.Cloneable : A class that implements the Cloneable interface must override the clone() method with a public method. Without implementing Cloneable interface if we invoke clone() method, it throws the ClassNotSupportedException.
java.rmi.Remote : A Remote interface is used to mark an object as remote that can be accessed from another machine. Any remote object must implement the interface directly or indirectly.
java.util.RandomAccess : RandomAccess interface is used by List implementations to indicate that they support fast (generally constant time) random access.
Marker interfaces have been deprecated since Java 5, they were replaced by annotations.
As per the Marker Interface definition, we can also create an interface without any method and a field means an empty interface known as Custom Marker Interface.
Let's see the example below and understand how to create Custom Marker Interface and use it with class
Example: Custom Marker Interface
Output
Mango is a fruit
Java introduced annotations an alternative of marker interface in JDK 1.5 Version. User Annotations we can achieve the same results as the marker interfaces. We can apply annotations to any class, and we can use them as indicators to perform certain actions.
Unlike annotations, interfaces allow us to take advantage of polymorphism. As a result, we can add additional restrictions to the marker interface.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
A marker interface doesn't have any data members and methods, then what is the use of it?
Marker interfaces are empty interfaces used to mark or identify a special operation. The Marker interface provides the run-time type information about an object, so the compiler and JVM have additional information about the object. For example, the Cloneable interface is used to mark cloning operations and the Serializable interface is used to mark the serialization and deserialization of an object.
What is the problem with the marker interface in java?
When any class implements a marker interface, all of its subclasses inherit them as well It's a common problem that occurs while using a marker interface in java. Since we cannot unimplement an interface in Java, therefore a subclass that does not want to treat differently will still be marked as a Marker.