Core JavaJava Interview Questions

What is a marker interface is Java

Posted On
Posted By admin

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.

 

 

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my Java course here

Also, if you'd like to test your Java knowledge, do check out my practice tests course here


If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment