Hello! This post is focused on marker interfaces in Java and how they are different forms of annotations. Every Java developer has seen them, although not everybody is aware that there is a specific word for this type of interface.

In this post, I would like to focus on the following points:

  • What is a marker interface
  • Quick Comparison between marker interface vs annotation
  • Which built-in marker interfaces do we have

What is a marker interface in Java?

Basically, marker interfaces are empty, they don’t have methods or fields (constants). Developers use them to provide run-time information about an object.

As an example, let me take a built-in marker interface java.lang.Cloneable. This interface does not clone a class, although, as you remember, there is a method Object.clone() that every Java class inherits.

So, by using of Cloneable interface you can indicate that a class has a clone behavior and it is legal to call its clone() method to perform a field-for-field copy of instances of that class.

Otherwise, when you will call clone() the method of a class that does not implement Cloneable, Java will throw the CloneNotSupportedException (and we need to implement exception handling)

Hope you got an idea of what is a marker interface. Let’s take a tour of built-in marker interfaces in Java first.

Built-in Marker interface in Java

Out of the box, Java provides 3 built-in marker interfaces for us:

  • java.lang.Cloneable
  • java.io.Serializable
  • java.rmi.Remote

You can review Javadoc for these interfaces to be sure that they perfectly suit the aforesaid definition of marker interface. As we already talk in the previous section about the Cloneable interface, let now give a floor to the remaining candidates.

java.io.Serializable is used to make objects serializable. Serialization is writing Java objects as a sequence of bytes and reading these objects from the stream of bytes.

It is implemented ObjectOutputStream for writing (or serialization) and via ObjectInputStream for reading (or deserialization). So in order to make a class serializable, we need to implement this marker interface.

Otherwise, we can receive NotSerializableException when for example calling ObjectOutputStream.writeObject(). By the way, all child classes (subclasses) of a serializable class are also serializable (we would talk about this in a moment).

java.rmi.Remote is a bit more complicated and rare case and it is ok if you have not heard about it before. It is used to identify interfaces whose methods may be invoked from a non-local virtual machine.

Any object that is a remote object must directly or indirectly implement this interface. Only those methods specified in a remote interface (this is an interface that extends java.rmi.Remote) are available remotely. You can read more about remote interfaces in this good Oracle tutorial on remote interface design.

Also, there is another concept in Java that may seem similar – annotations. In the next section, we will overview the difference between annotations and marker interfaces.

Marker interface vs Annotations

 Marker interface vs Annotation
Marker interface vs Annotation

Java annotations may seem similar to marker interfaces as they allow us to do the same thing. We can apply an annotation to any class to indicate that it can be used in a specific logic. And there is a strong voice to abandon marker interfaces at and switch to annotations. But are they 100% similar? Consider the following code snippet:

class Document implements Serializable{
    //..some fields
}

So, we made our document serializable and now can write/read it with serialization as we talk before. But let’s consider if we make several concrete documents:

class Spreadsheet extends Document{}
class Diagram extends Document{}
//...

Hey! – you will ask – what if we don’t want to make diagrams serializable? But we can’t in this configuration. Marker interfaces follow polymorphism logic and therefore all children of a serializable class are also serializable. Another logic would be if we would use some @Serializable annotation instead:

class Document{}
@Serializable
class Spreadsheet extends Document{}
class Diagram extends Document{}

By the way Document is also can be a marker interface, but we would talk about this in later posts.

Conclusion

In this short post, we discussed marker interfaces: what is it, what are built-in marker interfaces, and what is the difference between them and annotations. All Java developers to some degree encountered marker interfaces, but not everyone pays attention to that, in reality, it is a separate pattern. Hope this short article made you more interested in this concept. Have a nice day!

References

  • Gaurav Miglani. Marker interfaces in Java. GeeksForGeeks. Read here
  • Mainak Goswami. Is there a better approach to Marker? (2012). DZone. Read here
0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
1 year ago

[…] This interface can be implemented by a list of providers. Currently, we have only three types of employees permanent, contract, freelance. In the future, if we want to introduce one more type of employee in our system, we have to just create one more class and implement the same interface. […]