A bit on marker interfaces in Java

Hello again! This time I would like to talk about marker interfaces in Java. Every Java developer has seen them, although not everybody is aware that there is a specific word for this type of interfaces. In this post I would like to focus on following points: what is a marker interface, which built-in marker interfaces do we have and how they are compared with another concept – annotations.

What is a marker interface?

Basically, marker interfaces are empty, they don’t have methods or fields (e.g. constants). We use them to provide run-time information about an object. As an example, let me take a built-in marker interface java.lang.Cloneable. It does not clone a class itself, although there is a method Object.clone() that every Java class inherits. But by using of Cloneable interface we indicate that a class implements a clone logic and it is legal to call that clone() method to make a field-for-field copy of instances of that class. Otherwise, call of clone() method of class that does not implement Cloneable would lead to throwing of CloneNotSupportedException. Let take a look on built-in marker interfaces in Java first.

Built-in marker interfaces in Java

Java supplies 3 built-in marker interfaces for us:

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

You can follow Javadoc for these intefaces and be sure that they perfectly suit to the aforesaid definition of marker interface. So we already talk in the previous section about Cloneable interface. Let take a quick look on remaining interfaces.

java.io.Serializable is used to make objects serializable. Serialization is writing Java object as a sequence of bytes and reading these objects from the stream of bytes. It is implemented via ObjectOutputStream for writing (or serialization) and via ObjectInputStream for reading (or deseralization). 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 momemt).

java.rmi.Remote is a bit more complicated and rare, but it still makes sense to talk about it briefly here. This interface 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’s tutorial on remote interface design.

Also there is an another concept in Java that may seem similar – annotations. In next section let overview what is a difference between annotations and marker interfaces.

Marker interfaces vs. annotations

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

class Document implements Serializable{

    //..some fields
}

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

class Spreadsheet extends Document{}

class Diagram extends Document{}

//...

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 serializable class are also serializable. Another logic would be if we would use sample @Serializable annotation instead:

class Document{}

@Serializable
class Spreadsheet extends Document{}

class Diagram extends Document{}

By the way Document is also can be 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 a difference between them and annotations. All Java developers in some degree encountred marker interfaces, but not everyone pay 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