A marker interface in Java is an empty interface with no fields or methods. It has three types:
Serializable interface
Cloneable interface
Remote interface
Let’s discuss each of these examples:
The Serializable interface belongs to the java.io
package. It practices the concepts of serialization that make an object able to save its state in a file. Classes that do not implement a serializable interface will not have any of their states serialized or deserialized.
The following code implements the serializable interface:
import java.io.*;// By implementing Serializable interface// we make sure that state of instances of class A// can be saved in a file.class A implements Serializable{int i;String s;// A class constructorpublic A(int i,String s){this.i = i;this.s = s;}}class example{public static void main(String[] args)throws IOException, ClassNotFoundException{A a = new A(50,"Educative");// Serializing 'a'FileOutputStream fos = new FileOutputStream("xyz.txt");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(a);// De-serializing 'a'FileInputStream fis = new FileInputStream("xyz.txt");ObjectInputStream ois = new ObjectInputStream(fis);A b = (A)ois.readObject();//down-casting objectSystem.out.println(b.i+" "+b.s);// closing streamsoos.close();ois.close();}}
The Cloneable interface is present in the java.lang
package. There is a clone() method in the object class. Calling the object’s clone method on an instance of the class that does not implement the Cloneable interface throws an exception known as CloneNotSupportedException
. By default, classes that implement this interface should override the Object.clone()
method. The following code implements the Cloneable interface:
import java.lang.Cloneable;// By implementing Cloneable interface// we make sure that instances of class A// can be cloned.class A implements Cloneable{int i;String s;// A class constructorpublic A(int i,String s){this.i = i;this.s = s;}// Overriding clone() method// by simply calling Object class// clone() method.@Overrideprotected Object clone()throws CloneNotSupportedException{return super.clone();}}class example{public static void main(String[] args)throws CloneNotSupportedException{A a = new A(50, "Educative");// cloning 'a' and holding// new cloned object reference in b// down-casting as clone() return type is ObjectA b = (A)a.clone();System.out.println(b.i +" "+ b.s);}}
The Remote interface is present in the java.rmi
package. A remote object is an object that is stored in one machine and accessed from another. To make an object a remote object, it must be flagged with a remote interface. Here, the remote interface is used to identify interfaces whose methods may be called from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface. RMI (Remote Method Invocation) provides some convenience classes that remote object implementations can extend to facilitate the remote object creation.
To create a remote interface:
A user creates an interface that extends the predefined interface remote which belongs to the package, or they implement the Remote interface with the class that they want to make remote.
In this interface, all of the business methods are declared that can be called by the client.
Since there is a chance of network issues during remote calls, an exception named RemoteException may occur, throw it.
The following code implements the remote interface:
import java.rmi.Remote;import java.rmi.RemoteException;// Creating Remote interface for our applicationinterface Hello extends Remote {void printMsg() throws RemoteException;}
Free Resources