☰
|
B
|
Basic Level Interview Questions |
|
I
|
Intermediate Level Interview Questions |
|
A
|
Advanced Level Interview Questions |
Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object. The object can be restored at a later time, and even a later location. With persistence, we can move an object from one computer to another, and have it maintain its state.
Serialization is usually used when the need arises to send your data over network or stored in files. By data I mean objects and not text. Now the problem is your Network infrastructure and your Hard disk are hardware components that understand bits and bytes but not Java objects. Serialization is the translation of your Java object’s values/states to bytes to send it over network or save it.
No. Serializable is a Marker Interface. It does not have any methods.
The Java's java.io.Serializable is a marker interface, means that it contains no methods or fields and serves only to identify the semantics of being serializable. Therefore, a class implementing Serializable does not have to implement any specific methods . It is just used to "mark" java classes that their state can be serialized or deserialized.
So your classes must implement this if they are to be serialized and deserialized. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable.
public class MyClass implements Serializable {
// not implementing any specific java.io.Serializable methods because
// the interface has no methods to implement.
}
For a class to be serialized successfully, two conditions must be met:
java.io.Serializable interface.
For a class to be serialized successfully, two conditions must be met:
java.io.Serializable interface.
So if you want a class object to be serialized, all you need to do is implement the java.io.Serializable interface. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Java API takes care of the serialization protocol and persists the java object in a file in conformance with the protocol. De-serialization is the process of getting the object back from the file to its original form.
When serializing an object to a file, the standard convention in Java is to give the file a .ser extension.
Besides the Serializable interface, at least three alternate approaches can serialize Java objects:
Externalizable interface, which extends Serializable. By implementing Externalizable, a developer is responsible for implementing the writeExternal() and readExternal() methods. As a result, a developer has sole control over reading and writing the serialized objects.
If the object to be serialized includes references to the other objects, then all those object’s state also will be saved as the part of the serialized state of the object in question. The whole object graph of the object to be serialized will be saved during serialization automatically provided all the objects included in the object’s graph are serializable.
If you try to serialize an object of a class which implements serializable, but the object includes a reference to an non-serializable class then a NotSerializableException will be thrown at runtime.
No. The static variables belong to the class are not the part of the state of the object so they are not saved as the part of serialized object.
Variables marked transient are not included in the process of serialization and are not the part of the object’s serialized state.
It’s default value. e.g. if the transient variable in question is an int, it’s value after deserialization will be zero.
The values of the instance variables inherited from superclass will be reset to the values they were given during the original construction of the object as the non-serializable super-class constructor will run.
True
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.
Externalizable provides us writeExternal() and readExternal() method which gives us flexibility to control java serialization mechanism instead of relying on Java's default serialization. Correct implementation of Externalizable interface can improve performance of application drastically.
If Super Class of a Class already implements Serializable interface in Java then its already Serializable in Java, since you can not unimplemented an interface its not really possible to make it Non Serializable class but yes there is a way to avoid serialization of new class. To avoid Java serialization you need to implement writeObject() and readObject() method in your Class and need to throw NotSerializableException from those method. This is another benefit of customizing java serialization process.
One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.