Marker Interface Interview Questions
interface
In this post, we are going to discuss
- What is Marker Interface in Java?
- What Is The Use Of Marker Interface?
- What Are Built In Marker Interfaces?
- Can We Create Our Own Marker Interface? If Yes, How?
- What Are The Problems With Marker Interfaces?
- Can't We Achieve The Same Behaviour With Annotations?
What is Marker interface in Java?
Marker interface is the interface which doesn't contain any field or method. It is just an empty interface.
What is the use of marker interface?
If
Well. its name itself suggests that it is used for marking something, isn't it? It is used to convey to the JVM or compiler that all the classes, which implements particular marker interface, have some specific
They are also known as tag interfaces since they tag all the derived classes into a category based on their purpose.
For example,
All the class which Serializable
Serializable
NotSerializableException
In other words, just by marking a class Serializable
Marker interfaces are just an empty interfaces used to mark or identify a special operation. For example, Cloneable interface is used to mark cloning operation and Serializableinterface is used to mark serialization and deserialization of an object.
What are built in marker interfaces?
There are four main in-built marker interfaces in
- Serializable interface
Serializability of a class is enabled by the class implementing the java.io.Serializable interface.
- Cloneable interface
clone()
Cloneable
CloneNotSupportedException
- RandomAccess interface
It is used by List implementations to indicate that they support fast (generally constant time) random access.
- Remote interface
It serves to identify interfaces whose methods may be invoked from a non-local virtual machine.
Can we create our Own Marker Interface? If yes, how?
Yes, We can create our own marker interface just by creating a blank interface. In our code, we can use it as in method arguments or use it instanceof
public interface MyMarkerInterface {} public class MyMarkedClass implements MyMarkerInterface {}
Then you can MyMarkerInterface
instance:
public myMethod(MyMarkerInterface x) {}
or check with instanceof
at runtime like
if (obj instanceof MyMarkerInterface ) { // do something }
What are the Problems with marker interfaces?
One of the common
class A implements Serializable{} class B extends A {}
In above case, class B inheritably behaves Serializable
We need to handle such exceptional cases by throwing exceptions from child classes.
can't We achieve the same behaviour with Annotations?
Most of the thing that can be done with marker interfaces can be done with annotations. Annotations can have parameters of various kinds, and they're much more flexible. However there are some drawbacks of Annotations as well, For example,
-
To check if
object is an instance of an interface at runtime, one canuse instanceof
which is a relatively low-cost operation. Using annotations just to checktype of the object atrun time is very high as itrequires Java reflection
calls which is far more costly. Marker interfaces are better than annotations when they're used to define a type. For example,
Serializable
can be used (and should be used) as the type of an argument that mustbe serializable
. An annotation doesn't allow doing that:public void writeToFile(Serializable object);
Refer Item 37: Use marker interfaces to define types of Joshua Bloch's Effective Java to know more use cases and differences between Marker interface and marker annotations.
That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.
Happy Learning 🙂
If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.
We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.
About the Author
Tags: Core Java, Interface, Java, Java keywords
Comments and Queries
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:<pre><code class="java"> String foo = "bar"; </code></pre>For more information on supported HTML tags in disqus comment, click here.