Vous êtes sur la page 1sur 6

https://www.geeksforgeeks.

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.

Following are the well-known Marker Interfaces.

rmi.Remote
io.Serializable
lang.Cloneable

RandomAccess

RandomAccess interface is a Marker Interface

marker interface:- if an interface doesnt have any methods it is called marker


interface

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

Marker interface in Java


It is an empty interface (no field or methods). Examples of marker interface are
Serializable, Clonnable and Remote interface. All these interfaces are empty
interfaces.
public interface Serializable
{
// nothing here
}

Java�s built-in Marker Interfaces :


These are some built-in marker interfaces in java which are used to mark some
special behavior of a class.

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 :

This interface is used to mark serialization and deserialization of an object.


Serialization is a process in which an object state is read from memory and written
into a file or a database.

Deserialization is a process in which an object state is read from a file or a


database and written back into memory. Any class which wants it�s object to be
eligible for serialization and deserialization must implement 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.

1.Cloneable interface : Cloneable interface is present in java.lang package. There


is a method clone() in Object class. A class that implements the Cloneable
interface indicates that it is legal for clone() method to make a field-for-field
copy of instances of that class.

2.Serializable interface : Serializable interface is present in java.io package. It


is used to make an object eligible for saving its state into a file. This is called
Serialization.
Classes that do not implement this interface will not have any of their state
serialized or deserialized. All subtypes of a serializable class are themselves
serializable.
3.Remote interface : Remote interface is present in java.rmi package. A remote
object is an object which is stored at one machine and accessed from another
machine. So, to make an object a remote object, we need to flag it with Remote
interface. Remote interface serves to identify interfaces whose methods may be
invoked from a non-local virtual machine.Any object that is a remote object must
directly or indirectly implement this interface.

User Defined Marker Interfaces :


You can define your own marker interfaces to indicate about any special behavior.
Below is such an example. In this example, Cash and Checque are two marker
interfaces which are used to indicate whether payment is done by cash or checque.

interface Cash
{

interface Checque
{

class PaymentOne implements Cash


{
static void paymentByCash()
{
System.out.println("Payment is done by cash");
}
}

class PaymentTwo implements Checque


{
static void paymentByChecque()
{
System.out.println("Payment is done by checque");
}
}

public class MainClass


{
public static void main(String[] args)
{
PaymentOne.paymentByCash();

PaymentTwo.paymentByChecque();
}
}
Examples of Marker Interface which are used in real-time applications :

1.Cloneable interface : Cloneable interface is present in java.lang package. There


is a method clone() in Object class. A class that implements the Cloneable
interface indicates that it is legal for clone() method to make a field-for-field
copy of instances of that class.

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;

// 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 constructor
public A(int i,String s)
{
this.i = i;
this.s = s;
}

// Overriding clone() method


// by simply calling Object class
// clone() method.
@Override
protected Object clone()
throws CloneNotSupportedException
{
return super.clone();
}
}

public class Test


{
public static void main(String[] args)
throws CloneNotSupportedException
{
A a = new A(20, "GeeksForGeeks");

// cloning 'a' and holding


// new cloned object reference in b

// down-casting as clone() return type is Object


A b = (A)a.clone();

System.out.println(b.i);
System.out.println(b.s);
}
}
Output:

20
GeeksForGeeks

2.Serializable interface : Serializable interface is present in java.io package. It


is used to make an object eligible for saving its state into a file. This is called
Serialization.
Classes that do not implement this interface will not have any of their state
serialized or deserialized. All subtypes of a serializable class are themselves
serializable.
filter_none
edit
play_arrow

brightness_4
// Java program to illustrate 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 constructor
public A(int i,String s)
{
this.i = i;
this.s = s;
}
}

public class Test


{
public static void main(String[] args)
throws IOException, ClassNotFoundException
{
A a = new A(20,"GeeksForGeeks");

// 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

3.Remote interface : Remote interface is present in java.rmi package. A remote


object is an object which is stored at one machine and accessed from another
machine. So, to make an object a remote object, we need to flag it with Remote
interface. Here, Remote interface serves to identify interfaces whose methods may
be invoked 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
which facilitate remote object creation.

Vous aimerez peut-être aussi