What is a marker or tagged interface in Java?


This article will help you understand what a Marker or Tagged Interface in Java is. Before understanding marker interface, let’s revise on Interface.

INTERFACE

Similar to an object, an Interface is a blueprint of a class. It consists of static constants and abstract methods. It is a mechanism to achieve abstraction and multiple inheritance in Java. It is declared using the interface keyword. It provides total abstraction, meaning all methods in an interface must be declared with empty body, and all fields must be public, static and final by default.

Syntax

interface <interface_name>{
   // constant fields declaration
   // abstract methods declaration
   // by default
}

Marker Interface

An interface without methods, fields and constants is known as marker or tagged interface. This type oi interface is an empty interface. It delivers runtime information about an object. This information is delivered to both the JVM and the compiler. Two examples of marker interface are Serializable and Cloneable Interfaces.

Syntax

interface Serializable
{
}

Uses of Marker Interface

The Java compiler is informed by a message by the marker interface so that it adds some special characteristics to the class implementing it. If there is an information about a class which remains constant, then one can use marker interface to represent that information. It is mostly used for API development and in frameworks like Spring.

Types of Marker Interface

There exists many built-it marker or tagged interfaces in the JDK. The 3 most used interfaces are shown below −

  • Serializable Interface

  • Cloneable Interface

  • Remote Interface

Let us see each of these interfaces in details.

Serializable Interface

Serializable interface is a marker interface which is defined in java.io package. To make a class serializable, one must implement the Serializable interface. Then that object of that class can be serialized or deserialized according to requirement.

Serialization in Java is a way to convert the state of an object into a byte-stream. Its main usage is to travel object’s state on the network. Deserialization is the reverse process of Serialization where the object is reconstructed from the serialized state. One must have the object definition to successfully recreate it.

Syntax

ObjectInputStream in = new ObjectInputStream(inputStream);
Student s = (MyObject)in.readObject();

Example

Student.java import java.io.Serializable; // importing java.io package for serializable interface public class Student implements Serializable { // class declaration for implementing serializable interface int studentId; // variable declaration String studentName; // variable declaration public Student(int studentId, String studentName){ this.studentId = studentId; this.studentName = studentName; } } StudentSerialization.java import java.io.*;// java.io package imported class StudentSerialization{ // class declaration public static void main(String[] args) { // main function declaration try { // try block Student std = new Student(64,"JohnWick"); // creating object using Student class FileOutputStream fout=new FileOutputStream("Student File.txt"); // creating stream and writing the object ObjectOutputStream out=new ObjectOutputStream(fout); out.writeObject(std); out.flush(); out.close(); // closing the stream System.out.println("Data read from the file"); } catch(Exception e) { // catch block e.printStackTrace(); } } }

Output

Data read from the file

Cloneable Interface

Cloneable interface is a marker interface which is defined in java.lang package. It clones the object using a different name. The clone() method of the object class is supported by this interface. If this interface is not implemented in the class irrespective of invoking the clone() method, it will throw the ClassNotSupportedException.

Example

import java.util.Scanner; // java.util package imported public class Clone implements Cloneable { // class declaration along with cloneable interface int studentId; String studentName; int studentClass; public Clone (int studentId, String studentName, int studentClass) { //Clone class constructor this.studentId = studentId; this.studentName = studentName; this.studentClass = studentClass; } public void printList() { // function to print details System.out.println("Student ID: "+studentId); System.out.println("Student Name: "+studentName); System.out.println("Student Class: "+studentClass); } public static void main (String args[]) throws CloneNotSupportedException { // main function declaration Scanner sc = new Scanner(System.in); // Scanner class object created System.out.print("Enter Student ID: "); // Reading user given values int studentId = sc.nextInt(); System.out.print("Enter Student name: "); String studentName = sc.next(); System.out.print("Enter Student Class: "); int studentClass = sc.nextInt(); System.out.println("-------Student Detail--------"); Clone p1 = new Clone(studentId, studentName, studentClass); Clone p2 = (Clone) p1.clone(); //cloning the object of the Student class using the clone() method p2.printList(); // invoking the method to print detail } }

Output

Enter Student ID: 64
Enter Student name: JohnWick
Enter Student Class: 1
-------Student Detail--------
Student ID: 64
Student Name: JohnWick
Student Class: 1

Remote Interface

Remote Interface is a marker interface which is defined in java.rmi package. It marks an object as remote, which is accessible by another machine (host machine). An object can be used as a remote after implementing this interface. It also identifies interfaces whose functions can be invoked from JVM. The interface must be directly or indirectly implemented by any remote object.

Syntax

import java.rmi.*;
public interface Multiply extends Remote{
   public int mul(int p, int r, int t)throws RemoteException'
}

Custom Interface

Custom Interface can be created by user, i.e. it is not a built in interface like that of the above three.

Syntax

interface <interface_name>
{
}

Example

interface Bullet720 { // custom marker interface } class Train implements Bullet720 { // class that implements the Bullet720 marker interface static void isTrain() {// static function to display System.out.println("Bullet720 is a Train."); } } public class CustomInterface { // main class declaration public static void main(String args[]) { // main function declaration Train.isTrain(); // invoking methods of the class Train } }

Output

Bullet720 is a Train.

Updated on: 05-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements