Académique Documents
Professionnel Documents
Culture Documents
org/marker-interface-java/
https://javaconceptoftheday.com/marker-interface-java/
https://javahungry.blogspot.com/2014/03/hashmap-vs-hashtable-difference-with-
example-java-interview-questions.html
Marker Interface
The Serializable interface is a "marker" interface. This means that it has no
methods or fields, but simply "marks" a class as being able to be serialized.
rmi.Remote
io.Serializable
lang.Cloneable
RandomAccess
Marker interface is used as a tag to inform a message to the Java compiler so that
it can add special behaviour to the class implementing it. Java marker interface
has no members in it.
Lets take the java.io.Serializable marker interface. It does not have any members
defined it it. When a Java class is to be serialized, you should intimate the Java
compiler in some way that there is a possibility of serializing this java class. In
this scenario, marker interfaces are used. The java class which may be serialized
has to implement the java.io.Serializable marker interface. In such way, we are
intimating the java compiler
1) java.lang.Cloneable Interface :
This interface is used to mark the cloning operation. An object of a class which
implements Cloneable interface is eligible for field-by-field copying of an object.
2) java.io.Serializable Interface :
3) java.util.EventListener :
This is also a marker interface which must be extended by all event listener
interfaces.
4) java.rmi.Remote :
This is also a marker interface which is used to mark the invocation of a method
remotely. Only methods of those classes which implement Remote interface are
eligible for invocation by non-local virtual machine.
interface Cash
{
interface Checque
{
PaymentTwo.paymentByChecque();
}
}
Examples of Marker Interface which are used in real-time applications :
Invoking Object�s clone method on an instance of the class that does not implement
the Cloneable interface results in an exception CloneNotSupportedException being
thrown. By convention, classes that implement this interface should override
Object.clone() method.
import java.lang.Cloneable;
// A class constructor
public A(int i,String s)
{
this.i = i;
this.s = s;
}
System.out.println(b.i);
System.out.println(b.s);
}
}
Output:
20
GeeksForGeeks
brightness_4
// Java program to illustrate Serializable interface
import java.io.*;
// A class constructor
public A(int i,String s)
{
this.i = i;
this.s = s;
}
}
// 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 object
System.out.println(b.i+" "+b.s);
// closing streams
oos.close();
ois.close();
}
}
Output:
20 GeeksForGeeks