Popular Posts

Monday, January 30, 2012

Marker Interfaces In Java

An interface is called a marker interface when it is provided as a handle by java interpreter to mark a class so that it can provide special behavior to it at run-time and they do not have any method declarations.

The main purpose to have marker interfaces is to create special types in those cases where the types themselves have no behavior particular to them. If there is no behavior then why to have an interface? Because the implementer of the class might only need to flag that it belongs to that particular type and everything else is handled/done by some other unit – either internal to Java (as in the case of Java supplied standard marker interfaces) or an app specific external unit.

From java 1.5, the need for marker interface is eliminated by the introduction of the java annotation feature. So, it is wise to use java annotations than the marker interface. It has more feature and advantages than the java marker interface.

We cannot create marker interfaces, as you cannot instruct JVM to add special behavior to all classes implementing (directly) that special interface.

Serializable, Clonable, SingleThreadModel, EventListener, RandomAccess, Remote are Marker Interfaces.

Sources:
marker interface
what-is-a-java-marker-interface
marker-interface-in-java-what-why-uses-etc
how-marker-interface-is-handled-by-jvm
marker-interface-in-java-what-why-uses.html
are-marker-interfaces-dead
marker-interface-in-java.html

1 comment:

Anil Nivargi said...

Please go through this blog for more details about Marker Interface http://adnjavainterview.blogspot.in/2014/06/what-is-marker-interface-in-java-why.html.