The Wayback Machine - https://web.archive.org/web/20240606180821/https://www.geeksforgeeks.org/marker-interface-java/
Open In App

Marker interface in Java

Last Updated : 12 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

It is an empty interface (no field or methods). Examples of marker interface are Serializable, Cloneable and Remote interface. All these interfaces are empty interfaces. 
 

public interface Serializable 
{
  // nothing here
}

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. 
    Refer here for more details.
  2. Java




    // Java program to illustrate 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 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
  3. 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.
  4. Java




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

 



Previous Article
Next Article

Similar Reads

Why to Use Comparator Interface Rather than Comparable Interface in Java?
In Java, the Comparable and Comparator interfaces are used to sort collections of objects based on certain criteria. The Comparable interface is used to define the natural ordering of an object, whereas the Comparator interface is used to define custom ordering criteria for an object. Here are some reasons why you might want to use the Comparator i
8 min read
How to Add Custom Marker to Google Maps in Android?
Google Maps is used in many apps for displaying a location and indicating a specific location on a map. We have seen the use of maps in many apps that provides services such as Ola, Uber, and many more. In these apps, you will get to see How we can add Custom Marker to Google Maps in Android. What we are going to build in this article? We will be b
5 min read
How to Add OnClickListener to Marker on Google Maps in Android?
We have seen implementing Marker on Google Maps in Android. Now we will see adding OnClickListener for that marker on our Google Maps. In this article, we will take a look at adding OnClickListener to Google Maps marker in Android. What we are going to build in this article? We will be building a simple application in which we will be showing a mar
3 min read
MapmyIndia Android SDK - Display Marker
We have seen in many android application which displays a marker on their maps within their android application. In this article, we will take a look at How to display markers on Map My India Maps in the android application. Note: This Android article covered in both Java and Kotlin languages. Step by Step Implementation Step 1: Create a New Projec
4 min read
MapmyIndia Android SDK - Add Custom Marker
Many android applications use maps within their application. We will get to see that they display custom marker icons within their maps. In this article we, will take a look at How to add a custom marker to Map my India Maps in our android application. A sample video is given below to get an idea about what we are going to do in this article. [vide
6 min read
MapmyIndia Android SDK - Add Marker Clustering
Many times in android applications we get to see we have to display multiple markers. If the markers displayed within the maps are close to each other then we get to see that the markers overlap each other. In that case, many applications display the number of markers present at that location instead of displaying the marker. This method of display
7 min read
Java 8 | Consumer Interface in Java with Examples
The Consumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in one argument and produces a result. However these kind of functions don't return any value.Hence this functional interface which takes in one generic namely:-
4 min read
Java 8 | BiConsumer Interface in Java with Examples
The BiConsumer Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function that takes in two arguments and produces a result. However, these kinds of functions doesn't return any value. This functional interface takes in two generics, namely:- T:
5 min read
Java 8 | DoubleToIntFunction Interface in Java with Example
The DoubleToIntFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in a double-valued argument and gives an int-valued result. The lambda expression assigned to an object of DoubleToIntFunction type is used to define
1 min read
Java 8 | IntToDoubleFunction Interface in Java with Examples
The IntToDoubleFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in an int-valued argument and gives a double-valued result. The lambda expression assigned to an object of IntToDoubleFunction type is used to define
1 min read
Article Tags :
Practice Tags :