Can anybody tell me where the methods of marker interfaces are written? As because the marker interfaces contain no method. i.e : For Cloneable() interfaces where the clone() method is written?
Thanks in Advance,
Joined: Mar 22, 2005
Since marker interfaces do not contain methods, there's no implementation to point to. See this FAQ entry for some more information on marker interfaces.
clone is defined in the Object class, but it is not part of the Cloneable interface. Cloneable merely governs whether clone may or may not be called. (That of course begs the question: why wasn't clone made part of Cloneable, and that interface implemented by all cloneable classes, instead of adding the method to the Object class?) [ January 10, 2008: Message edited by: Ulf Dittmer ]
Originally posted by Parmeet Singh: Implmentation for marker iterfaces is provided by the JVM as in most of the cased implementation is native.
No. The implementation of marker interfaces can be provided by the JVM, and it can be native, but generally it will be implemented by Java code in a user class or a class in the Java Class Libraries. Remember that implementing Serializable is about deciding which classes may be serialized, not about actually serializing them. See here for some other such interfaces.
Joined: Feb 09, 2006
Remember that implementing Serializable is about deciding which classes may be serialized, not about actually serializing them.
I Agree with this. totally.
but who actually serialized it, where actually it get it serialized. isnt it in JVM. JVM knows which object to serialized by checking if that object implements Serializable interface.
Please assist if am wrong.
Joined: Mar 22, 2005
but who actually serialized it, where actually it get it serialized. isnt it in JVM.
Yes, serialization is implemented in the JVM. But you were talking about marker interfaces in general being implemented in the JVM, and that is not true.
JVM knows which object to serialized by checking if that object implements Serializable interface.
The check for whether to serialize a class is not done in the JVM, it is part of the java.io.ObjectOutputStream class. If you search its source code for "instanceof Serializable" you'll find that check, and the code that throws an exception if the object to be serialized doesn't implement Serializable.
Joined: Jul 11, 2001
Isn't the whole serialization process implemented in the ObjectOutputStream class (and classes used by it)?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Jan 30, 2000
The source code for ObjectOutputStream shows that, as of JDK 6, there are a couple of native methods, but the vast majority of serialization is accomplished by regular Java code inside ObjectOutputStream and inside ObjectStreamClass (which also has just a couple native methods). Of course ObjectStreamClass uses a lot of reflection, and those reflective methods are often native, so there's no single clear answer to this. I'd say most of serialization is implemented in Java code, and some isn't.
Also note that Externalizable is a subinterface of Serializable that does contain methods, allowing people to write their own serialization code in Java classes. So serialization may be accomplished that way as well, and if you do that, you are both deciding that the class may be serialized, and also deciding how to serialize it.
But more generally, whatever is done with marker interfaces, the code that uses them may be regular Java code, or native code. I think regular Java code is much more common, but does it really matter in general? Both are possible, and people using the code really don't need to know or care in most cases. The fact that Serializable is implemented a certain way tell s us nothing about Cloneable or other marker interfaces.