Monday, June 30, 2008

Few Tips On Serialization

  1. Static fields are like the Transient fields and they are not serialized with the object. The reason is that they are associated with the class rather then object's state.
  2. If we have a sub class which extends a super class and implements Serializable interface and super class doesn't implement Serializable interface then:
  • While serializing the sub class object, only its state excluding the super class visible variables will be serialized.
  • While de-serializing the sub class state will restored from the serialized data. The super class state will be initialized by invoking the no-arg constructor of super class.
  • The no-arg constructor should be visible to the sub class otherwise an exception will be thrown at runtime.
  1. If the super class also implements Serializable interface then the state of the subclass object including the super class visible variables will be serialized. And while de-serializing the sub class and super class state will be restored from the serialized data only instead of no-arg constructor for super class.
  2. If there is an arg constructor in the super class and there is no default constructor then the subclass constructor should specify the call to the specific super (arg...) constructor. Otherwise there will be a compile-time error.