Marker Interface in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

An interface that does not have any methods, fields, or constants, i.e, an empty interface in java is known as Marker or Tag Interface.

It is used to deliver type information at runtime to the JVM so that it can take some action based on the information received.

One of the main purposes behind the ideology of marker interfaces is that they are used to convey a message to the JVM that the class implementing this type of interface has some extra functionalities.

Popular examples of marker interface in java include Cloneable, Serializable, Remote Interface.

What is Marker Interface?

Marker interfaces are interfaces that don't have fields, methods, or constants. In other terms, a marker interface or tag interface is an empty interface. It provides information about an object's run-time type.

The JVM and compiler have additional information about an object because of this. Examples of marker interfaces include the Serializable and Cloneable interfaces. It denotes a signal or command to the JVM, to put it briefly.

The interface for a marker must be empty, but the declaration is the same as for an interface in Java. Following is the syntax of Marker Interface in Java : 

Syntax of Marker Interface in Java

Alternatives of Marker Interface

  • Interface in Java is an abstract type that is used to define a behavior that class(s) must implement.

Note: An abstract data type is a data type where only behavior is defined but not implemented. Defining a behavior without any implementation means declaring a function by defining it.

  • Marker Interface in Java is nothing but just a special type of interface which is made empty on purpose.

As marker interfaces in Java can be indicated as a signal or command to the JVM, so can't there be any other way in which we can signal the JVM? Yes, there are alternatives of Marker Interfaces in Java.

Main alternatives of Marker Interfaces include:

  1. Internal Flags: Internal flags in a class can be used to indicate to the JVM that some extra special operations need to be allowed to the respective class.

Example:

Explanation:
Here, the variable named allowSpecialBehavior is set to be true. This can be used by JVM to assign some special behavior to the objects of this class.

  1. Annotations: Annotations in Java act as a tag that represents the metadata attached with class(s), interface(s), or method(s) to indicate some additional information that can be used by JVM to grant some special operations to the respective class.

Example:

Explanation: Here, the @Service annotation used above class A definition tells JVM that class A is a Service class. A Service class is used by a client to interact with the functionalities in your application.

Uses of Marker Interface

The main use of the Marker Interface in Java is to convey to the JVM that the class implementing some interface of this category has to be granted some special behavior.

e.g, when a class implements the Serializable interface, which is a marker interface, then this is an indication to the JVM that the objects of this class can be serialized. Similarly, when a class implements Cloneable Interface, then it indicates to the JVM that the objects of this class can be cloned.

Note: Cloning is the process of creating an exact copy of an object. Serialization is the process of conversion of an object into a static stream (sequence) of bytes, which can be saved to a database or transferred over a network.

Examples of Marker Interface in Java

JDK Marker Interface in Java Examples are:

java.io.Serializable, java.lang.Cloneable, java.rmi.Remote, javax.servlet.SingleThreadModel, javax.ejb.EnterpriseBean, java.util.RandomAccess, java.util.EventListener, etc. We will look at 3 of the most popular marker interface in Java:

a.) Cloneable Interface: Cloneable Interface in Java is a marker interface. It belongs to java.lang package. It is used to create a clone or copy of an object with some other name. If we want to clone the objects of a class, then that class has to implement the Cloneable interface. This is used to indicate the JVM that the clone() method of the Object class can be used by the objects of the class which have implemented the Cloneable Interface.

 Cloneable Interface

Syntax:

Point to Note: If a class has not implemented the Cloneable Interface, and its objects call the clone() method, then it throws an exception named as CloneNotSupportedException.

Example 1: In the below example, we are going to notice that a class that has not implemented the Cloneable interface, and the objects of such a class invoke the clone() method, then JVM throws an exception at the runtime.

Output:

Explanation:
Here, we have a Plant class, and we have created an object p1 of the Plant class. And we are trying to clone the object p1 with another name as p2. But since the Plant class has not implemented the Cloneable Marker Interface, the execution stops at runtime with CloneNotSupportedException.

Example 2:
The below example is the same as the previous example only. Just that this time the Plant class has implemented the Cloneable Interface. And so the objects of the Plant class can invoke the clone() function without any problem.

Output:

Explanation: Here, we have a Plant class, and we have created an object p1 of the Plant class. And we are trying to clone the object p1 with another name as p2.

Since the plant class has implemented the Cleanable Marker Interface in Java, so clone of p1 is made and stored in p2.

b.) Serializable Interface: Serializable Interface is a Marker Interface in Java, i.e, it is an empty interface as it has no methods, fields or constants, etc. Its definition is present in Java's IO package.

 Serializable Interface

Syntax:

Note: Serialization is the process of conversion of the state of an object into a byte stream so that it can be saved on the disk or in a database or shared over a network. On the other hand, deserialization does the opposite, i.e, conversion of a byte stream into an object.

If we want to save the state of an object of a class into physical memory (e.g, a file) or retrieve an object's state which is already stored in the physical memory (e.g, a file), then we must make it Serializable. In order to make a class Serializable, it needs to implement the Serializable Interface.

Example: In the below example, we are demonstrating that a class that implements the Serializable marker interface, makes its objects eligible to be serialized and deserialized.

Output:

Explanation:
Here, we have created a Student class that is implementing the Serializable Interface. This allows the objects of the Student class to be serialized and deserialized. In the main function, we have created an object s1 of the class Student and stored it on the disk in a file named S1_Info.txt.

And just after that we have retrieved the same s1 object from the file named S1_Info.txt and stored it in s2.

c.) Remote Interface:
Remote Interface is a marker interface in Java, which is defined in java.rmi package. It is used for Remote Method Invocation purposes.

Note: Remote Method Invocation or RMI is a mechanism that allows an object residing in one system JVM to access/invoke an object running on another system JVM.

Remote Interface

Syntax:

Example: The below example shows the use of Remote Interface to demonstrate the phenomenon of Remote Method Invocation. Here an object is created and bound (using rebind() function) with a URL on the localhost server. This stub is accessed from another java file by using the lookup() function, and then using this stub the function named as checkEvenOdd() is invoked with the parameter as an integer variable.

Output(s): Terminal 1 (For Server)

Output(s): Terminal 2 (For Client)

Explanation:
Here, we have created an EvenOddCheckerInterface which extends the Remote Interface, thereby making the subclasses of EvenOddCheckerInterface as RMI enabled. A remote class named EvenOddCheckerRemote is created which implements the EvenOddCheckerInterface, which provides the definition for checkEvenOdd() method.

There is a server class and a client class created. The server class creates an object of EvenOddCheckerRemote and binds it to a URL. The client creates a stub and accesses the EvenOddCheckerRemote object using the EvenOddCheckerInterface instance.

And when it access the object in the stub, it calls the checkEvenOdd() method with an integer parameter, for which it gets a boolean value referring to an even or odd number, and accordingly, it prints its result.

2.) Custom Marker Interface in Java: We can create our own Marker Interface in Java, as shown in the below example.

Example: In the below example, we have created our own custom marker interface. We have made 2 classes Employee and Customer. We are classifying the objects of these 2 classes in 2 different lists, using the marker interface.

Output(s):

Explanation:

Here we have created a marker interface with the name EmployeeMarkerInterface. Employee class implements the marker interface, whereas the Customer class does not. In the main function, we have created 2 employees and 2 customers.

We are segregating employees and customers using the marker interface.

Marker Interfaces vs Annotations

Annotations in Java can be applied to any class so that the JVM can assign special behavior to the respective class. This makes us realize that both Marker Interfaces and Annotations are the same things. But the point here to note is that they are not completely similar.

One of the major differences is due to Polymorphism. Marker Interfaces follow the concepts of polymorphism.

If we make an Interface as Marker Interface, and some class has implemented this marker interface, then all the child classes that extend this class will automatically and indirectly have implemented the marker interface. This behavior is not there with Annotations.

This fact can sometimes be used as an advantage by the programmer, and sometimes it can be a disadvantage as well.

Example:

There is a class named as KnowledgeContent, which is made Serializable (Marker Interface) as shown below in the code.

There are 2 child classes of KnowledgeContent class, i.e, Text, and Video. We want only the Text class to become Serializable, and not the Video class.

But since Marker Interfaces follow polymorphism and therefore all children of a serializable class are also serializable. So Text and Video classes both are Serializable now, as shown below:

But if we want to make only the Text class as Serializable and not the Video class, so we will have to use @Serializable Annotation, as shown below:

The above example shows when polymorphism in Marker Interfaces can act as a disadvantage. But what if we had a requirement in which we wanted that all the child classes should also become Serializable if the parent class is Serializable.

In that case, polymorphism in Marker Interfaces can act as an advantage.

Why? Because if we use Annotations, then we will have to add @Serializable manually above the definition of each child class, and that will be a very boring repetitive task.

Marker Interfaces vs Typical Interfaces

Normal interfaces can also be used as a marker interface, but there will be issues with that. Let's find out by understanding the below example.

Example: Let's see one example where we are allowing only those objects to be a deletable whose class or some parent class has implemented the Deletable Marker Interface.

Here we have allowed the deletion of only those objects which are directly or indirectly implementing the Marker Interface 'DeletableEmployee'. But can we not do the same using a normal Interface. Let's find out below:

Here we have allowed deletion of the objects of the classes which have implemented the typical non-marker interface Employee. But if another type of instance comes in the future, say, Consultant, shown as below:

And if we want to allow the Objects of classes that have implemented the Consultant interface also to be deleted but without using the marker interface way, then we will have to modify the logic of the delete() function of the EmployeeDao class as follows:

But what if in the future maybe we want to accommodate other instances as well like Freelancer, ContractEmployees, etc to be allowed deletion, then each time we will have to modify the logic of our delete() method of EmployeeDao class. Thus, opting for the Marker Interface way is a wise choice.

Conclusion

  • Marker Interface in Java (also known as Tag Interface) are empty interfaces, i.e, they do not have any variables or methods declared in them.
  • Marker Interfaces in Java act as an indication for the JVM to allow some special functions to the class(s) which have implemented them.
  • Few of the most popular marker interfaces are Cloneable, Serializable, and Remote Interfaces.
  • Cloneable Interface is used to indicate to the JVM that the objects of the class implementing the Cloneable Interface can be cloned by invoking the clone() function.
  • Serializable Interface, on the other hand, is used to indicate to the JVM that the objects of the class implementing the Serializable interface can be serialized, i.e, their state can be stored in the memory or shared over a network.
  • Remote Interface is used to indicate to the JVM that the objects of the class implementing the Remote Interface can be accessed using the JVM of a Remote System. This enables the Remote Method Invocation phenomenon for the methods of the class implementing the remote interface.