Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marker Interfaces in Java?

People also ask

What is marker interface explain with example?

In other words, an empty interface is known as marker interface or tag interface. It delivers the run-time type information about an object. It is the reason that the JVM and compiler have additional information about an object. The Serializable and Cloneable interfaces are the example of marker interface.

How many marker interfaces are available in Java?

It has three types: Serializable interface. Cloneable interface. Remote interface.

What is tag or marker interface in Java with example?

It is also known as a tagging interface and is used to indicate or inform the JVM that a class implementing this interface will have some special behaviour. An efficient way to classify code can be achieved using the marker interface. Examples of such an interface are: Serializable, Cloneable and Remote Interface.

What is functional and marker interface in Java?

A marker interface is an interface declaring no methods, a functional interface is one with only one abstract method.


  1. Is the definition of a marker interface mentioned above in 1st point wrong? - It is correct in the parts that (1) a marker interface must be empty, and (2) implementing it is meant to imply some special treatment of the implementing class. The part that is incorrect is that it implies that JVM or the compiler would treat the objects of that class differently: you are correct in observing that it is the code of Java class library that treats these objects as cloneable, serializable, etc. It has nothing to do with the compiler or the JVM.
  2. instead of using the instanceOf operator why can't the method be something like writeObject(Serializable) so that there is a compile-time type checking - This lets you avoid polluting your code with the name of the marker interface when a "plain Object" is needed. For example, if you make a class that needs to be serializable, and has object members, you would be forced to either do casting or make your objects Serializable at compile time. This is inconvenient, because the interface is devoid of any functionality.
  3. How Annotations are better than Marker Interfaces? - They let you achieve the same purpose of conveying metadata about the class to its consumers without creating a separate type for it. Annotations are more powerful, too, letting programmers pass more sophisticated information to classes that "consume" it.

It is not possible to enforce Serializable on writeObject because children of non-serializable class can be serializable, but their instances may be upcasted back to the parent class. As a result, holding a reference to something non-serializable (like Object) does not mean that the referred instance cannot be really serialized. For instance in

   Object x = "abc";
   if (x instanceof Serializable) {
   }

the parent class (Object) is not serializable and would be initialised using its parameter-less constructor. The value referenced by x, String, is serializable and the conditional statement would run.


I have made a simple demonstration to resolve doubt no 1 and 2 :

We will be having Movable interface which will be implemented by MobilePhone.java Class and one more class LandlinePhone.java which do NOT implement Movable interface

Our marker Interface:

package com;

public interface Movable {

}

LandLinePhone.java and MobilePhone.java

 package com;

 class LandLinePhone {
    // more code here
 }
 class MobilePhone implements Movable {
    // more code here
 }

Our Custom Exception Class : package com;

public class NotMovableException extends Exception {

private static final long serialVersionUID = 1L;

    @Override
    public String getMessage() {
        return "this object is not movable";
    }
    // more code here
    }

Our Test class : TestMArkerInterface.java

package com;

public class TestMarkerInterface {

public static void main(String[] args) throws NotMovableException {
    MobilePhone mobilePhone = new MobilePhone();
    LandLinePhone landLinePhone = new LandLinePhone();

    TestMarkerInterface.goTravel(mobilePhone);
    TestMarkerInterface.goTravel(landLinePhone);
}

public static void goTravel(Object o) throws NotMovableException {
    if (!(o instanceof Movable)) {
        System.out.println("you cannot use :" + o.getClass().getName() + "   while travelling");
        throw new NotMovableException();
    }

    System.out.println("you can use :" + o.getClass().getName() + "   while travelling");
}}

Now when we execute main class :

you can use :com.MobilePhone while travelling
you cannot use :com.LandLinePhone while travelling
Exception in thread "main" com.NotMovableException: this object is not movable
    at com.TestMarkerInterface.goTravel(TestMarkerInterface.java:22)
    at com.TestMarkerInterface.main(TestMarkerInterface.java:14)

So which ever class implements marker interface Movable will pass the test else error message will be displayed.

This is the way instanceOf operator check is done for Serializable, Cloneable etc


A marker interface in Java is an interface with no fields or methods. Put more simply, an empty interface in Java is called a marker interface. Examples of marker interfaces are the Serializable, Cloneable and Remote interfaces. These are used to indicate some information to compilers or JVMs. So if the JVM sees that a class is Serializable, it can do some special operation on it. Similarly, if the JVM sees some class is implementing Cloneable, it can perform some operations to support cloning. The same is true for RMI and the Remote interface. So in short, a marker interface indicates a signal or a command to the compiler or JVM.

The above started out as a copy of a blog post but has been lightly edited for grammar.