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.

marker interface

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.