What is a Marker Interface?

 In Java, a marker interface is an interface that contains no methods or fields and is used to mark a Java class. Marker interfaces are also known as tag interfaces.

The purpose of a marker interface is to provide a way to associate metadata with a class, without requiring the class to actually implement any specific behavior. For example, the Serializable interface is a marker interface that is used to indicate that a Java class can be serialized, or converted into a stream of bytes for storing or transmitting over a network.

To use a marker interface in your code, you simply need to implement the interface in your class declaration, like this:

public class MyClass implements Serializable { // class implementation goes here }

Then, you can use the instanceof operator to check whether an object is an instance of a class that implements the marker interface.

MyClass obj = new MyClass(); if (obj instanceof Serializable) { // obj can be serialized }


Comments

Popular posts from this blog

Can we make the main() thread a daemon thread?

What are the Memory Allocations available in JavaJava?

What is the default value stored in Local Variables?