A bit about marker interfaces in Java
Tags: java, beginners, tutorial, architecture
Published: 2019-07-27 08:51:57

---
title: A bit about marker interfaces in Java
published: true
description: 
cover_image: https://www.mednikov.net/wp-content/uploads/2019/07/brush-colors-colorful-colourful-1129653.jpg
tags: java, beginners, tutorial, architecture
---

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 anotherr 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](https://docs.oracle.com/javase/tutorial/rmi/overview.html). 

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](https://javarevisited.blogspot.com/2012/01/what-is-marker-interfaces-in-java-and.html) at all and switch to annotations. But are they 100% similar? Consider a following code snippet:

```java
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:

```java
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:

```java
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. You can also read the original version of this post [about marker interfaces in Java](https://www.mednikov.net/tutorials/a-bit-on-marker-interfaces-in-java/) in my blog. Have a nice day!

## References

* Gaurav Miglani. _Marker interfaces in Java_. GeeksForGeeks. [Read here](https://www.geeksforgeeks.org/marker-interface-java/) 
* Mainak Goswami. _Is there a better approach to Marker?_ (2012). DZone. [Read here](https://www.javacodegeeks.com/2012/10/is-there-a-better-approach-to-marker.html)