This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Can anyone please let me know how the marker interface provides the functionality to its object implementation?
What i know is that Marker interfaces doesn't define any members. its an empty interface without any methods then how it provides the functionality to the objects which implements this marker interface.
Marker interfaces are used to 'mark' your objects. A good example is java.io.Serializable interface. If you make your own marker interface, you can check whether your objects are 'marked' by using an instanceOf check, as in if(myObject instanceOf Serializable)
A marker interface does not provide any functionality to a class by itself. You can use instanceof to check if an object is of a class that implements the marker interface, and then do whatever you want to do:
In the standard Java library, marker interfaces are used in a few places. The most well-known are java.io.Serializable and java.lang.Cloneable. You can use these on your own classes to indicate that it should be possible to serialize or clone objects of your class. Other code in the standard Java library then uses instanceof to check for these marker interfaces when you try to serialize or clone an object of your class.
but i want to know that if your class is serialized using serialized interface then how the state of the objects of the serialized class saved, means where the methods writeObjedct() and and readObject() are defined and implemented to save the state of the objects?
The writeObject and readObject are optional methods that you can implement in your class. When they are there, they will be called by the JVM when your object is serialized or deserialized. If you don't implement these methods, then the JVM will serialize and deserialize the object using a default format.
These methods are not defined in any interface anywhere. They are special methods that the JVM recognizes. This doesn't really have anything to do with how marker interfaces work; it's just the way how serialization is built-in into Java.
Note that the FAQ entry Wouter linked to states that you should not create any new marker interfaces; it's what annotations are used for nowadays.
Jesper wrote:The most well-known are java.io.Serializable and java.lang.Cloneable. You can use these on your own classes to indicate that it should be possible to serialize or clone objects of your class.
I agree, yet these two, especially Cloneable, are also a major reason why so many people get confused over the whole issue of marker interfaces. Cloneable is about allowing access to the clone method, which is a really weird design: the interface should have had the clone method in it (thus not being an actual marker interface).
Jesper wrote:(The readObject and writeObject) methods are not defined in any interface anywhere. They are special methods that the JVM recognizes. This doesn't really have anything to do with how marker interfaces work; it's just the way how serialization is built-in into Java.
Yeah, and it's a bad design IMO. Serialization customization should arguably have been left to the Externalizable interface.
Ping & DNS - updated with new look and Ping home screen widget