wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is the need for marker interface ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the need for marker interface ? " Watch "What is the need for marker interface ? " New topic
Author

What is the need for marker interface ?

Shaik Muhammad
Ranch Hand

Joined: Jul 16, 2008
Posts: 36
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

Joined: Oct 13, 2005
Posts: 38441
    
  23
See whether this FAQ page helps.
Shaik Muhammad
Ranch Hand

Joined: Jul 16, 2008
Posts: 36
Is it a good idea/design to use Marker interface if it doesn't have any abstract method?
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

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.



Regards, Sud.
SCJP 5 ScjpFAQ JLS
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
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.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Shaik Muhammad
Ranch Hand

Joined: Jul 16, 2008
Posts: 36
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

Joined: Apr 02, 2008
Posts: 823

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

Joined: Oct 13, 2005
Posts: 38441
    
  23
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

Joined: Apr 02, 2008
Posts: 823

annotations? :roll:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Annotations.

Moving to the beginner forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What is the need for marker interface ?