Saturday, February 11, 2012

Marker interface

an interface with no methods is called as marker interface

ex:
interface test{ }

Serializable, Clonable, SingleThreadModel are called marker interfaces in java. these marker interface's give additional information about the behavior of the class.


Marker interface is an empty interface. Eg is Serializable. When a class
implements this interface then only we can serialize the object. i.e
we can save in the file. In other words any class which implements
serializable can be serialized which is the additional feature when
compared with an ordianary object.

Simillarly when a class is implementing java.rmi.Remote it can be
used as a Remote object.


Reference:
http://geekexplains.blogspot.com/2009/10/marker-interface-in-java-what-why-uses.html

  • Since the ability of a Class's data to be serialized and de-serialized correctly is implementation specific, it is necessary to be able to assert whether serialization is possible for this class.
That is why the Marker Interface is provided and utilized.

For example, if your class members contain system-dependant information that is only relevant on one machine (such as hardware info) or only during the scope of one app server's life-cycle (like session Ids) then you are provide the ability to NOT mark it as serializable, so that no code will attempt to serialize and de-serialize, which would result in logic or runtime errors later on.
Whereas if your data consists of transportable data such as names or phone numbers, you can mark it serializeable, thus indicating that all manners of transport utilizing serialization and de-serialization may be employed on this data to allow it to be moved between machines etc.

http://www.linkedin.com/groups/Why-serialization-use-marker-interface-70526.S.5853381965264211970?view=&gid=70526&type=member&item=5853381965264211970&trk=eml-anet_dig-b_nd-pst_ttle-cn

No comments:

Post a Comment