2

I am not able to clear this confusion of mine,

Externalizable is used because we can give our own (more efficient) implementation of serialization-deserialization using read/writeExternal method. And control the serialization process.

We can also use read/writeObject method and serialization process will be overridden with that.

Then why is Externalizable even required?

Why would someone prefer read/writeExternal over read/writeObject?

RaT
  • 1,134
  • 3
  • 12
  • 28
  • How exactly would you override serialization process with `readObject` or `writeObject`? – lexicore Mar 28 '18 at 10:29
  • 1
    As per my understanding, we can give our own implementation of serialization process in readObject and java will call readObject method if it is present instead of using its own implementaion. Similarly for writeObject. – RaT Mar 28 '18 at 10:58
  • So you would implement a custom `ObjectOutputStream` or `ObjectInputStream`? – lexicore Mar 28 '18 at 11:00
  • The default ObjectStream will be used – RaT Mar 28 '18 at 11:06

1 Answers1

1

Just the customization in case of serialization.

The reason is given in Externalizable docs

The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods. Object Serialization uses the Serializable and Externalizable interfaces. Object persistence mechanisms can use them as well. Each object to be stored is tested for the Externalizable interface. If the object supports Externalizable, the writeExternal method is called. If the object does not support Externalizable and does implement Serializable, the object is saved using ObjectOutputStream. **

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • But all these control and customization can be achieved by read/writeObject as well. What extra Externalizable is giving? – RaT Mar 28 '18 at 10:55
  • @RahulTyagi Assume that you need to save your Object to a file and at the same time you have to have a custom serialization ? In that case, writeExternal will be used for serialization and writeObject for writing your object to file. – Suresh Atta Mar 28 '18 at 10:59
  • But only writeObject could work for both, isn't it? – RaT Mar 28 '18 at 11:08
  • Could work if you do not implement Externalizable and no further customization required while serializing. – Suresh Atta Mar 28 '18 at 11:09
  • That is the question, if externalizable interface were not even exist what would have we missed? We could achieve everything using read/write Object method. – RaT Mar 28 '18 at 11:14
  • Again, if you want to have separate implementation just for serialization and not for any other purpose, you use externalizable interface and *external methods. – Suresh Atta Mar 28 '18 at 11:15