Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does marker interfaces work?

 
Siddharth bal sharma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean, i know its just a hint to the JVM that "ok buddy- its your responsiblity to take care of this special behavior of the object" , but how does it really work?

Is there any call-back mecanism in place , now u might think that marker interfaces dont have any methods so which call back methods! But my marker interface can be extended by another interface X.Now that X implementation is the JVM implementation class or some native implementation or JNI( for serializable)?

small class-dia just to depict what i mean by my question.
extends
X----------->serializable
A
| implements
|
|
someclass
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

You can recognize an object that implements an interface by using "instanceof". If you look at the source of java.io.ObjectOutputStream, you can see lines like "if (obj instanceof Serializable)" that do certain things fro serializable objects, and different things for other objects.
 
Siddharth bal sharma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i asked abt how the marker interfaces are implemented i.e. they act as marker for JVM, but how does JVM tales care of it?
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same way as any other interface a class implements, it lists the class as implementing that interface.
How another class would use a class implementing your marker interface depends entirely on the programmer of that class.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Ulf pointed out you use the instanceof operator to check if an object is serialized. Any subclasses that extend the class that you have serialized are also serializable.

Example:


blah b = new blah();

Now - b instanceof Serializable will return true.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic