Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the need for marker interface ?

 
Shaik Muhammad
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marker interface is the interface without any abstract methods. What is the need to use it then? Why did Sun came up with this?
 
Campbell Ritchie
Sheriff
Posts: 51419
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See whether this FAQ page helps.
 
Shaik Muhammad
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it a good idea/design to use Marker interface if it doesn't have any abstract method?
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marker or Tag interface has no methods in it to be implemented.

e.g Serializable,Remote.

This tell the JVM that the class implementing the marker interface
has the property of,defined by,the interface.

Like for serialization, the interface is Serializable,
which announces that the class implementing it is serializable,i.e the object's state can be saved.

Same is the case with Remote(in case of RMI), which tells that the user defined interface,
which extends Remote interface,will contain the methods that the object
will be calling remotely.

 
arulk pillai
Author
Ranch Hand
Posts: 3393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently. Example java.io.Serializable, java.lang.Cloneable, java.util.EventListener etc. Marker interfaces are also known as �tag� interfaces since they tag all the derived classes into a category based on their purpose.
 
Shaik Muhammad
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you All. Would like to know what exactly does the approach of JVM/Compiler towards the MarkerInterfaceImplementer class will change the implementer class' compilation or execution. What will the JVM/ Compiler do by knowing this? What is really achieved by letting the JVM/Compiler know about this behaviour of the implemented class?
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By special behavior it means that,for example say, Serializable, JVM will know that the class implementing Serializable, the state of the object of the class can be saved.

By saving the state we mean that the JVM can write the state of object(that is defined by the value of the instance variables) to a file or the serialized object can be shipped to an another place, say over the wire to another machine and can be de-serialized.

This property of an object being serialized comes when the user tells the JVM that the class which he/she is writing is Serializable.And when the user serializes the object, rest of the work is done by the JVM internally,as it is pre declared that the Object being serialized is of sreializable type.It gets the special treatment of Serialization.
 
Campbell Ritchie
Sheriff
Posts: 51419
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But (I think the FAQ points this out) now that annotations are available, people will no longer create new marker interfaces. Marker interfaces are also sometimes called tagging interfaces.
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
annotations? :roll:
 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Annotations.

Moving to the beginner forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic