Serialization in java is a mechanism of writing the state of an object into a byte stream. It is mainly used in Hibernate, RMI, JPA, EJB, JMS technologies.he String class and all the wrapper classes implements java.io.Serializable interface by default.
For More Details on Serialization and Interview Questions on Serialization go to Serialization in Java
1) Superclass is serialized but subclass is not serialized
If parent class is implementing Serializable interface than all its child becomes serializable automatically. So no need to explicitly implement serializable interface in sub class.
2) Superclass is not serialized but subclass is serialized
If superclass is not serializable and subclass object is serialized then state of the member variable of subclass will be saved but not the superclass. Output of the member variable of superclass will be default value. the value which will explicitly set before serialization will not reflected in superclass output .
3) Both superclass and subclass is serialized
If both superclass and subclass is serialized then state of member variable of both the class will be saved.
4) Both superclass and subclass is not serialized
Not Serializable exception will be thrown as shown below:-
java.io.NotSerializableException: com.java.serialization.Serder
An object is serializable (itself implements the Serializable interface) even if its superclass is not. However, the first superclass in the hierarchy of the serializable class, that does not implements Serializable interface, MUST have a no-arg constructor. If this is violated, readObject() will produce a java.io.InvalidClassException in runtime.
For More Details on Serialization and Interview Questions on Serialization go to Serialization in Java
1) Superclass is serialized but subclass is not serialized
If parent class is implementing Serializable interface than all its child becomes serializable automatically. So no need to explicitly implement serializable interface in sub class.
2) Superclass is not serialized but subclass is serialized
If superclass is not serializable and subclass object is serialized then state of the member variable of subclass will be saved but not the superclass. Output of the member variable of superclass will be default value. the value which will explicitly set before serialization will not reflected in superclass output .
3) Both superclass and subclass is serialized
If both superclass and subclass is serialized then state of member variable of both the class will be saved.
4) Both superclass and subclass is not serialized
Not Serializable exception will be thrown as shown below:-
java.io.NotSerializableException: com.java.serialization.Serder
An object is serializable (itself implements the Serializable interface) even if its superclass is not. However, the first superclass in the hierarchy of the serializable class, that does not implements Serializable interface, MUST have a no-arg constructor. If this is violated, readObject() will produce a java.io.InvalidClassException in runtime.
No comments:
Post a Comment