Some common examples of marker interfaces are Serializable, Cloneable, and Remote.
Since a marker interface does not define any fields, methods, or behavior, you might wonder why Java needs it. The purpose of a marker interface is to provide metadata to the JVM or frameworks, indicating that a class possesses a particular capability or should receive special handling during runtime.
What Is the Purpose of a Marker (Tag) Interface in Java
If you look closely at marker interfaces in Java, such as Serializable, Cloneable, and Remote, you'll notice that they are used to indicate a special capability or characteristic of a class to the JVM or framework.
For example, when the JVM detects that a class implements Serializable, it enables Java's serialization mechanism for that class. Similarly, if a class implements Cloneable, the JVM allows the Object.clone() method to create a field-by-field copy of the object. In the case of RMI, implementing the Remote interface indicates that the object can be invoked remotely.
In short, a marker interface serves as a marker that provides metadata to the JVM or framework, indicating that the class should receive special handling.
A common interview follow-up question is: "Why not use a boolean flag or a String field inside the class instead of a marker interface?"
Although the same information could be represented using a boolean flag or a String, marker interfaces offer several advantages. They make the purpose of a class more explicit and readable, enable compile-time type checking, and allow APIs to leverage polymorphism by accepting only objects that implement a particular marker interface. These benefits make marker interfaces a cleaner and more object-oriented design choice.
Where Should I use Marker interface in Java :--
Apart from the built-in marker interfaces such asSerializable and Cloneable, you can also create your own marker interfaces. A marker interface is a good way to logically classify your code. By creating custom marker interfaces, you can group related classes and, if you have your own framework or tool, perform pre-processing or special operations on the classes that implement those interfaces. Marker interfaces are particularly useful when developing APIs and frameworks.However, since the introduction of annotations in Java 5, annotations have become a better alternative to marker interfaces in many scenarios. JUnit is a perfect example of this approach, where the @Test annotation is used to identify test methods. The same functionality could have been achieved using a Test marker interface, but annotations provide a more flexible and expressive solution.
Another use Of Marker Interface in Java:
Another use of marker interfaces in Java is to communicate design intent. For example, a marker interface named
ThreadSafe can be used to indicate to other developers that any class implementing this interface is expected to be thread-safe. Any future modifications to such classes should preserve this thread-safety guarantee.
Marker interfaces can also be used by custom code analysis, code coverage, or code review tools to identify classes with specific behavior and perform additional validation. However, since the introduction of annotations in Java 5, annotations have become a better choice for these use cases. For example, using a @ThreadSafe annotation is much more expressive and flexible than implementing a ThreadSafe marker interface.
Is Runnable a Marker Interface?
A common interview question is whether the Runnable interface is a marker interface.
The answer is No. Runnable is not a marker interface because it declares the public void run() method. A marker interface does not contain any methods or fields.
Can We Create Our Own Marker Interface?
Another frequently asked question is whether we can create our own marker interface.
The answer is Yes. Although we cannot make the JVM treat a custom marker interface the same way it treats built-in marker interfaces such as Serializable or Cloneable, we can create our own marker interfaces and implement custom logic around them. For example, a framework or utility can check whether a class implements a particular marker interface and perform specific processing based on that.
Summary
In summary, marker interfaces in Java are used to indicate special behavior or metadata to the compiler, JVM, frameworks, or custom tools. However, since Java 5, annotations have become the preferred approach because they are more expressive, flexible, and easier to use than marker interfaces in most scenarios.
Ohayo,
ReplyDeleteMuchas Gracias Mi Amigo! You make learning so effortless. Anyone can follow you and I would not mind following you to the moon coz I know you are like my north star.
I want to make a project with a simulator of an enigma machine, the one used in the world war 2, but i actually don't know where to begin, because i don't know java very well.
In particular, having an array like that char[] sRotor = {'G','N','U','A','H','O','V','B','I','P','W','C',' J','Q','X','D','K','R','Y','E','L','S','Z','F','M' ,'T'};, how can i say, in few codes, if a letter is x, turn it into y, if a letter is z, turn it into t... This for all the letters, creating like pairs.
Then, talking about the rotors, when someone presses a letter, the first rotor should move, changing completely the code; when the first rotor changes from 26 (the last letter of the alphabet) to 1, the second rotor should move. Obviously, the same for the second and the third rotor. How can i write that?
Very useful post !everyone should learn and use it during their
Learning path.
Thanks and Regards
Hola,
ReplyDeleteWhat a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this.
I have two methods and they do almost same thing, but at some most inner nested level one of method have additional command.
Java Code:
method2(){
... same code ...
for {
... same code ...
if{
... same code ...
}
}
}
Follow my new blog if you interested in just tag along me
in any social media platforms!
Shukran,
Your wrong!!! I do not know where you are getting this information at? From java SE 7 Programmers Study Guide mentions that “interface is a public set of methods that must be implemented by the class that uses the interface.” An interface is an interface, if it has changed I should read this in this type of book mention above. If an interface like Cloneable has no public methods, means it does not have public methods, but any class that implements it must implement the Object.clone() as public. This is what it means!!! Please, don’t make something so simple into a complex discussion, especially at an interview!!
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteUse of marker interface in java
ReplyDeleteMarker Interfaces are used to indicate something to compiler/JVM. If JVM see that a class is a object of Marker Interface then it will perform some special operation. Take an example with Serializable, Clonnable marker interface, if JVM see a Class is Serialized/Clonnable then It will do some special operation on it, similar way if JVM sees one Class is implemented custom marker interface which is created by ourself then the JVM do some special operation. How it’d do the special operation,