What is the use of marker interfaces in Java?

JavaAnnotationsMarker Interfaces

Java Problem Overview


When there is nothing to implement in the marker interfaces like Serializable What is the use of implementing it?

Java Solutions


Solution 1 - Java

Joshua Bloch: Effective Java 2nd Edition, p 179

>Item 37: Use marker interfaces to define types > > ... > You may hear it said that marker > annotations (Item 35) make marker > interfaces obsolete. This assertion is > incorrect. Marker interfaces have two > advantages over marker annotations. > First and foremost, marker interfaces > define a type that is implemented by > instances of the marked class; marker > annotations do not. The existence of > this type allows you to catch errors > at compile time that you couldn’t > catch until runtime if you used a > marker annotation....

Personally I think I'll bow to Joshua's superior knowledge on this subject.

Solution 2 - Java

In earlier versions of Java, Marker Interfaces were the only way to declare metadata about a class. For example, the Serializable Marker Interface lets the author of a class say that their class will behave correctly when serialized and deserialized.

In modern Java, marker interfaces have no place. They can be completely replaced by Annotations, which allow for a very flexible metadata capability. If you have information about a class, and that information never changes, then annotations are a very useful way to represent it.

Solution 3 - Java

Such marker interfaces are useful in the case other code takes decisions depending on whether an object implements some marker interface.

In the case of Serializable, reflection will be used to serialize the fields of the objects.

Now annotations are preferred as they don't propagate to sub-classes.

See Marker interface pattern.

Solution 4 - Java

It indicates that the class (and consequently all the fields which aren't transient) are candidates for serialisation. And if you're building a framework reliant on serialisation, you can of course write a method thus:

public void registerObject(Serializable obj);

to limit the classes you're prepared to accept.

Because a serialized object needs to retain compatibility across systems, serialisation is an explicit design decision and hence requires the use of the marker interface, to identify such candidates.

There's also a security aspect. You don't want to make everything serialisable - otherwise you can accidentally expose (say) passwords or other sensitive data via serialisation.

Solution 5 - Java

They are called marker interfaces. And as the name implies, they mark that some object is available for certain sort of operations.

Serializable means that the object is eligible for java serialization, for example.

It has been discussed whether they shouldn't be replaced by annotations, since their functions are quite similar.

Solution 6 - Java

If you implement an interface then instanceof will be true. If you interface has nothing to implement then you can use this to mark a class with meta-data like annotations do for Java 1.5 and up without having to force the implementor to do anything special.

Solution 7 - Java

You are right in reasoning that an empty interface does not affect the "standard" execution of the program which is based on inspection/mutation of fields and dispatching of methods.

However, marker interface are useful when used in conjunction with reflection: A library/method inspects (via reflection) an object and works differently if its class impplements the marker interface. As of Java5 there's very little need for marker interfaces - the same "marking" facility can be achieved via Java annotations - which (again) most of their effect will be achieved via reflection-based code.

Solution 8 - Java

The main purpose is to tell the compiler that treat differently for the object of the class which implemented marker interface.

Solution 9 - Java

Looking carefully on marker interface in Java e.g. Serializable, Clonnable and Remote it looks they are used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it done some special operation on it, similar way if JVM sees one Class is implement Clonnable it performs some operation to support cloning. Same is true for RMI and Remote interface. So in short Marker interface indicate, signal or a command to Compiler or JVM.

Read more: http://javarevisited.blogspot.com/2012/01/what-is-marker-interfaces-in-java-and.html#ixzz2v6fIh1rw

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGuruKulkiView Question on Stackoverflow
Solution 1 - JavaT CView Answer on Stackoverflow
Solution 2 - JavaChris PitmanView Answer on Stackoverflow
Solution 3 - JavaGregory PakoszView Answer on Stackoverflow
Solution 4 - JavaBrian AgnewView Answer on Stackoverflow
Solution 5 - JavaBozhoView Answer on Stackoverflow
Solution 6 - JavacyborgView Answer on Stackoverflow
Solution 7 - JavaItay MamanView Answer on Stackoverflow
Solution 8 - JavaChanikagView Answer on Stackoverflow
Solution 9 - Javauser3293519View Answer on Stackoverflow