Marker Interface in Java
A marker interface in Java is an interface with no methods or properties, serving only as a marker or tag to indicate a specific characteristic or behaviour of a class that implements it. The primary purpose of a marker interface is to enable runtime processing based on the presence or absence of the marker interface. It is used to tag a class with a certain capability or behaviour that can be identified at runtime using the instanceof operator.
Approach: Using Cloneable interface
The Cloneable interface is a marker interface that is used to indicate that a class can be cloned. A clone is an exact copy of an object, including all its fields and state.
FileName: Main.java
// MyClass implements the Cloneable interface to indicate that it can be cloned class MyClass implements Cloneable { private int value; public MyClass(int value) { this.value = value; } public int getValue() { return value; } // Overrides the clone() method from the Object class to make MyClass cloneable @Override public Object clone() throws CloneNotSupportedException { // Calls the super.clone() method to create a shallow copy of the object return super.clone(); } } public class Main { public static void main(String[] args) { // Creates an instance of MyClass with value 42 MyClass myObj = new MyClass(42); try { // Clones myObj to create a duplicate object MyClass myObjClone = (MyClass) myObj.clone(); // Prints the values of the original and cloned objects to verify that cloning works System.out.println("Original value: " + myObj.getValue()); System.out.println("Cloned value: " + myObjClone.getValue()); } catch (CloneNotSupportedException e) { // If MyClass is not cloneable, a CloneNotSupportedException will be thrown System.out.println("Clone not supported"); } } }
Output:
Original value: 42 Cloned value: 42
Approach: Using Serializable interface
The Serializable interface is used to indicate that a class can be serialized, i.e., its object can be converted into a stream of bytes for storage or transmission over a network. To implement the Serializable interface, a class simply declares that it implements the interface, without implementing any methods or properties.
FileName: Main.java
import java.io.*; // MyClass implements the Serializable interface to indicate that it can be serialized class MyClass implements Serializable { private int value; public MyClass(int value) { this.value = value; } public int getValue() { return value; } } public class Main { public static void main(String[] args) { // Creates an instance of MyClass with value 42 MyClass myObj = new MyClass(42); try { // Creates a new FileOutputStream to write serialized data to a file named "myObj.ser" FileOutputStream fileOut = new FileOutputStream("myObj.ser"); // Wraps the FileOutputStream in an ObjectOutputStream for writing objects ObjectOutputStream out = new ObjectOutputStream(fileOut); // Writes the serialized form of the object to the file out.writeObject(myObj); // Closes the ObjectOutputStream and FileOutputStream out.close(); fileOut.close(); // Prints a message to confirm that the serialization was successful System.out.println("Serialized data is saved in myObj.ser"); } catch (IOException e) { // Catches any IOExceptions that occur during serialization and prints a stack trace e.printStackTrace(); } } }
Output:
Serialized data is saved in myObj.ser