![]() |
VOOZH | about |
A Marker Interface in Java is an interface that contains no methods or fields. It is used to mark a class so that the Java runtime or compiler can identify some special behavior or capability of that class.
Syntax:
public interface Serializable { }
Here, Serializable has no methods but if a class implements it, Java knows that objects of that class can be serialized.
Marker interfaces give information to the JVM or compiler about:
They function like a flag for the runtime environment.
Person object created Animal object created Person is serializable Animal is not serializable
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:
Cloneable interface is a marker interface in java.lang that allows an object to be cloned using Object.clone(). If a class does not implement Cloneable, calling clone() on its object throws CloneNotSupportedException.
20 GeeksForGeeks
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.
20 GeeksForGeeks
Remote interface (in java.rmi) is a marker interface that identifies objects whose methods can be invoked remotely from another JVM. Any class implementing Remote is eligible to be used in Java RMI, allowing distributed communication between systems.
Explanation: Extending Remote marks the interface as eligible for remote invocation. Each remote method must declare throws RemoteException.