Use of Marker Interfaces in Java



An interface with no methods in it is referred to as a marker interface, also known as a tagging interface. There are two basic design purposes of marker interfaces.

Creates a common parent 

It is used to provide a common parent interface for a group of related interfaces.

When an interface like "eventlistener" is extended by dozens of other interfaces in the Java API, we can use a marker interface to create a common parent among a group of interfaces. For example, when an interface extends EventListener, the JVM knows that this particular interface is going to be used in an event delegation scenario.


Adds a data type to a class

This situation is where the term, tagging comes from. A class that implements a tagging interface does not need to define any methods (since the interface does not have any), but the class becomes an interface type through polymorphism.

There are two commonly used marker interfaces in Java as follows:

  • Serializable Interface
  • Cloneable Interface

Serializable Interface

The Serializable interface is used to mark classes whose objects can be serialized. It is the process of converting an object into a byte stream, which can be saved to a file or transmitted over a network.

Example

The following program uses serialization to show how to save and retrieve a 'Person' object. It saves (serializes) the Person object to a file named 'person.ser' and then reads (deserializes) it back from the file, displaying both the original and retrieved objects. The Person class implements 'Serializable', allowing its instances to be converted to a byte stream.

import java.io.*;

class Person implements Serializable {
   private static final long serialVersionUID = 1L;
   private String name;
   private int age;

   public Person(String name, int age) {
      this.name = name;
      this.age = age;
   }

   @Override
   public String toString() {
      return "Person{name='" + name + "', age=" + age + "}";
   }
}

public class SerializableExample {
   public static void main(String[] args) {
      Person person = new Person("Revathi", 23);
   
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
         oos.writeObject(person);
         System.out.println("Serialized: " + person);
      } catch (IOException e) {
          e.printStackTrace();
      }
   
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser"))) {
         Person deserializedPerson = (Person) ois.readObject();
         System.out.println("Deserialized: " + deserializedPerson);
      } catch (IOException | ClassNotFoundException e) {
          e.printStackTrace();
      }
   }
}

Output

Following is the output of the above program ?

Serialized: Person{name='Revathi', age=23}
Deserialized: Person{name='Revathi', age=23}

Cloneable Interface

The Cloneable interface indicates that a class allows its objects to be cloned. It creates a new instance of the class with the same values as the original instance.

Example

This program demonstrates cloning an 'Employee' object using the 'Cloneable' interface. The Employee class overrides the clone() method to enable object cloning. The 'CloneableExample' class creates an original Employee object and clones it, then prints the original and cloned objects to verify that they are identical.

class Employee implements Cloneable {
   private String name;
   private int id;

   public Employee(String name, int id) {
      this.name = name;
      this.id = id;
   }

   @Override
   protected Object clone() throws CloneNotSupportedException {
      return super.clone();
   }

   @Override
   public String toString() {
      return "Employee{name='" + name + "', id=" + id + "}";
   }
}

public class CloneableExample {
   public static void main(String[] args) {
      try {
          Employee emp1 = new Employee("Indu", 101);
          Employee emp2 = (Employee) emp1.clone();
          
          System.out.println("Original Employee: " + emp1);
          System.out.println("Cloned Employee: " + emp2);
      } catch (CloneNotSupportedException e) {
          e.printStackTrace();
      }
   }
}

Output

The above program displays the following output ?

Original Employee: Employee{name='Indu', id=101}
Cloned Employee: Employee{name='Indu', id=101}
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2024-12-23T11:16:25+05:30

612 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements