Return to site

JAVA INTERVIEW QUESTION: Explain the difference between Externalizable and Serializable?

· java
  • Externalizable defines custom rules for serialization. 
  • Externalizable extends Serializable. 
  • Externalizable defines writeExternal and readExternal methods to create your own serialization contract. 
  • These two methods readExternal and writeExternal supersedes readObject and writeObject --they call these later ones--. 
  • In object de-serialization the public no-argument constructor is used to reconstruct the object, Externalizable object must have a public no-argument constructor. --whereas Serializable does not-- 
  • In most real time scenarios, you can use Serializable and write your own custom implementation for serialization by providing readObject and writeObject. 
  • You may need Externalizable if you are not happy with the way java writes/reads objects from stream, or if you need special handling for supertypes on object construction during serialization.