Marker Interfaces provide runtime information about the objects. They declare metadata about the class.
Following examples can be given for Marker Interfaces in Java:
Following examples can be given for Marker Interfaces in Java:
- Serailzable
- Cloneable
- EventListner
JVM is responsible for implementing the functionality specified by such interfaces. eg By implementing a class with Serializable enables us to save the object as JVM takes care that when a call to writeObject() is made, all non-transient objects will be saved.
If marker interface is user defined, the only purpose they solve is to "tag" the class. Annotations can also be used to solve the purpose. In that case OperatonNotSupported type of exceptions will arise at run time as annotations are checked at runtime thru refection, where as marker interfaces can be checked at compile time.This feature definitely gives them more weightage over annotations.
eg
******* Start: CASE-1 ********
@Interface
JavaDeveloper{
}
@Interface
ITStaff
@JavaDeveloper
class Developer{
}
public void action(Object obj){
if(obj.getClass().isAnnotationPresent(JavaDevelper.class))
//do something
else
//do something else
}
If marker interface is user defined, the only purpose they solve is to "tag" the class. Annotations can also be used to solve the purpose. In that case OperatonNotSupported type of exceptions will arise at run time as annotations are checked at runtime thru refection, where as marker interfaces can be checked at compile time.This feature definitely gives them more weightage over annotations.
eg
******* Start: CASE-1 ********
@Interface
JavaDeveloper{
}
@Interface
ITStaff
@JavaDeveloper
class Developer{
}
public void action(Object obj){
if(obj.getClass().isAnnotationPresent(JavaDevelper.class))
//do something
else
//do something else
}
******* End: CASE-1 ********
******* Start: CASE-2 ********
interface JavaDeveloper{
}
class Developer implements JavaDeveloper{
public void action(Developer developer){
}
This behavior can also be achieved by a maintaining a field in the class, but tagging the class with interface makes it more readable.
******* End: CASE-2 ********
******* Start: CASE-2 ********
interface JavaDeveloper{
}
class Developer implements JavaDeveloper{
public void action(Developer developer){
}
This behavior can also be achieved by a maintaining a field in the class, but tagging the class with interface makes it more readable.
******* End: CASE-2 ********
In CASE-1, input arg of action() is of type Object & real type is only determined at run time as compared to CASE-2 in which action is passed Developer as input arg, thus checking the arg type at compile time itself.
Marker Interfaces vs Normal Interfaces
Marker interfaces are different from normal interfaces in the sense that Normal Interfaces describes a "is a" relationship between interface and a class. But a Marker Interface just tells the user that the implementing class has a special property: a condition forced by Marker Interface.
Marker Interfaces vs Normal Interfaces
Marker interfaces are different from normal interfaces in the sense that Normal Interfaces describes a "is a" relationship between interface and a class. But a Marker Interface just tells the user that the implementing class has a special property: a condition forced by Marker Interface.
Comments