What is a marker interface is Java

In this blog post, I will be explaining what is a marker interface in Java.

 

Marker Interface Definition

A marker interface is simply an interface that has no methods. Some examples of marker interfaces in Java are Serialiazable , Cloneable,  etc.

 

How Marker Interfaces work

So if there are no methods in a marker interfaces, how do they work? In other words, how does a class that implements a marker interface know what methods to provide?

The answer to this is that marker interface specify a design pattern  used to provide run-time type information about the objects.  The very fact that a class implements a marker interface indicates something to the JVM or compiler. So when the JVM sees an object of the marker Interface type, it will perform some special operation.

You can also define your own marker interfaces and write code that behaves in a special way if the marker interface is implemented. So for ex. objects that belong to a class that implements the marker interface can follow one path of execution in your code and objects that do not implement the marker interface can follow another path of execution.

 

 

Comments

Popular posts from this blog

How to use logging in SpringBoot with code samples

Python While Loop with code samples

How to convert a List to a Set