• Register

Marker interface in Java

It is an vacant interface (no field or function). Samples of marker interface are Serializ-able, Clonn-able and Remote interface. All these interfaces are unfilled interfaces.

public interface Serializable 
{
  // nothing to do  here
}

Samples of Marker Interface which are used in real-time applications :

Cloneable interface : This interface is a current in java.lang package. There is a function clone() in obj class. A class that applies the Cloneable interface specifies that it is lawful for clone() function to make a field-for-field duplicate of instances of that class.
Calling Obj clone function on an occurrence of the class that does not apply the Cloneable interface outcomes in an exception CloneNotSupportedException being thrown. By resolution, classes that applieis this interface should supersede Obj.clone() function.


Example of the code:

demonstrate Cloneable interface 
import java.lang.Cloneable; 
  
// By applying Cloneable interface 
// we make surity that instances of class A1 
//it  can be cloned. 
class A1 implements Cloneable 
{ 
    int i1; 
    String s1; 
  
    // A1 class constructor 
    public A1(int i1,String s1) 
    { 
        this.i1 = i1; 
        this.s1 = s1; 
    } 
  
    // Superseding clone() method 
    // by simply invoking Obj class 
    // clone() function. 
    @Override
    protected Object clone() 
    throws CloneNotSupportedException 
    { 
        return super.clone(); 
    } 
} 
  
public class Test1 
{ 
    public static void main(String[] args) 
        throws CloneNotSupportedException 
    { 
        A a1 = new A(20, "Kodlogs"); 
  
        // cloning 'a1' and allotment 
        // new cloned obj1 orientation in b1 
  
        // down casting as clone() yield type is Obj1 
        A b1 = (A)a1.clone(); 
  
        System.out.println(b1.i1); 
        System.out.println(b1.s1); 
    } 
} 

Output:

20

KodLogs

2,760 points
7 4