A class implements Serializable interface and then serializes its object into a flat file. Subsequently, the class
definition is altered and an attempt is made to de-serialize the object from the flat file. What would be the result of
this attempt?
Answers
Answer:
Java object serialization (writing) is done with the ObjectOutputStream and deserialization (reading) is done with the ObjectInputStream. That Serializable is a marker interface means that it contains no methods. Therefore, a class implementing Serializable does not have to implement any specific methods.
Answer:
The attempt to deserialize the object after altering the class definition will result in a “java.io.InvalidClassException".
Explanation:
A "NotSerializableException" will be issued at runtime if you attempt to serialise an object of a class that implements serializable but the object contains a reference to a non-serializable class.
The default Java serialisation process records object metadata, such as the class name, field names and types, and superclass, during object serialisation. The serialised object includes this class declaration as part of its storage. The deserialization procedure can recreate the objects and map the stream data into the class attributes with the correct type thanks to the saved metadata.
The Java serialisation mechanism automatically generates a hash value each time an object is serialised. The secure hash algorithm (SHA) is used in the ObjectStreamClass's computeSerialVersionUID() method to generate a hash value from the class name, sorted member names, modifiers, and interfaces. Suid is another name for the serialVersionUID.Therefore, when the serialised object is recovered, the JVM checks the said value of the serialised class with the value of the object. The object is considered to be compatible with the class and is therefore deserialized if the suid values match.
The reason for this exception is that the class definition present in the flat file is not compatible with the altered class definition in the current JVM. Serialization uses the class definition at the time of serialization to store the object state, and if the class definition changes, it may not be able to restore the object state. To resolve this issue, a class versioning mechanism should be implemented.
For more such question: https://brainly.in/question/13792074
#SPJ3