Marker interface(Serializable, Clonnable&Remote)
Last updated
Was this helpful?
Last updated
Was this helpful?
It is an empty interface(no field or methods). Examples of marker interface are Serializable, Clonnable and Remote interface. All these interfaces are empty interfaces.
Marker interface in Java is used to indicate something to Compiler, JVM or any other tool but Annotation is better way of doing same thing.
Serialization
(
Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in out program.
If you want a class object to be serializable, all you need to do it implement the java.io.Serializable
interface. Serializable in java is a marker interface and has no fields or methods to implement. It's like an Opt-In process through which we make our classes serializable.
Serialization in java is implemented by ObjectInputStream
and ObjectOutputStream
. So all we need is a wrapper over them to either save it to file or send it over the network.
If you want an object property to be not serialized to stream, you can use transient keyword like I have done with salary variable.
transientis a Java keyword which marks a member variable not to be serialized when it is persisted to streams of bytes. When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred; they are lost intentionally.
readObject(ObjectInputStream ois): If this method is present in the class, ObjectInputStream readObject() method will use this method for reading the object from stream.
writeObject(ObjectOutputStream oos): If this method is present in the class, ObjectOutputStream writeObject() method will use this method for writing the object to stream. One of the common usage is to obscure the object variables to maintain data integrity.
Object writeReplace(): If this method is present, then after serialization process this method is called and the object returned is serialized to the stream. This method allows the developer to provide a replacement object that will be serialized instead of the original one.
Object readResolve(): If this method is present, then after deserialization process, this method is called to return the final object to the caller program. One of the usage of this method is to implement Singleton pattern with Serialized classes. Read more at ( The problem with serialized singleton class is that whenever we deserialize it, it will create a new instance of the class. to overcome this scenario all we need to do it provide the implementation of readResolve()
method.
readResolve() :is used for replacing the object read from the stream. The only use I've ever seen for this is enforcing singletons; when an object is read, replace it with the singleton instance. This ensures that nobody can create another instance by serializing and deserializing the singleton.
Another frequently asked Serialization interview question. This is sometime also asked as what is the use of transient variable, does transient and static variable gets serialized or not etc. so if you don't want any field to be part of object's state then declare it either static or transient based on your need and it will not be included during Java serialization process.
to be continue...