the purpose of marker interface in java


J

jrefactors

I know what is marker interface - An interface with no methods.
Example: Serializable, Remote, Cloneable.

But I don't know the purpose of using marker interface, and when is the
good practice for that.

Please advise. thanks!!
 
Ad

Advertisements

S

Stefan Schulz

I know what is marker interface - An interface with no methods.
Example: Serializable, Remote, Cloneable.

But I don't know the purpose of using marker interface, and when is the
good practice for that.

Please advise. thanks!!

For 1.5, i would not use marker interfaces at all anymore. Use a
Runtime-Retained annotation.

Generally, they are used to give additional information about the
behaviour of a class. The possibly best example is the RandomAccess
interface... the worst offender probably Cloneable.
 
C

Chris Uppal

Stefan said:
For 1.5, i would not use marker interfaces at all anymore. Use a
Runtime-Retained annotation.

Just BTW, has anyone looked at the performance of the runtime annotations ?

-- chris
 
M

Matt Humphrey

Stefan Schulz said:
For 1.5, i would not use marker interfaces at all anymore. Use a
Runtime-Retained annotation.

Generally, they are used to give additional information about the
behaviour of a class. The possibly best example is the RandomAccess
interface... the worst offender probably Cloneable.

Although I rarely use marker interfaces, I like that they're logically
consistent with the language--it's simply an interface that has no
properties. I find the class annotations like those in C# (e.g.
[Serializable] [STAThread], etc) to be more like hacks applied outside of
the language to bridge the gap between the language and the supposedly
abstract runtime environment. It would be nice if there could be a more
consistent or language-centric way to specify and manipulate these features.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
T

Tor Iver Wilhelmsen

Stefan Schulz said:
For 1.5, i would not use marker interfaces at all anymore. Use a
Runtime-Retained annotation.

Except that

object instanceof MarkerInterface

is easier than

object.getClass().isAnnotationPresent(MarkerAnnotation.class)
 
T

Thomas G. Marshall

Tor Iver Wilhelmsen coughed up:
Except that

object instanceof MarkerInterface

is easier than

object.getClass().isAnnotationPresent(MarkerAnnotation.class)


I think so too. I'm not sure why someone would want to avoid it.

I'm assuming that it has to do with an aversion to the following:

1. RTTI
2. downcasting

But there are times when both are the clearest thing to do.
 
Ad

Advertisements

Joined
Aug 19, 2006
Messages
4
Reaction score
0
1.>in general tagged or marker interface is used to just identify the class, in real time projects there are so many classes,in order to identify which class belongs to which module,this tag or marker interface is useful.

2.>

in serialization also,if we want to serialize the object,first it will internally checks whether the class is implementing java.io.serializable interface by using instanceof operator.

3.>in rmi also every remote class should implement java.rmi.remote interface.

public interface csedept{

}

public class computer implements csedept{

int no;

String name;

}

public class Sample

{

computer c=new computer();

if(c instanceof csedept)

{

c.name="computer science course";

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top