What do you need Externalizable for?

What do you need Externalizable for? You can perform your own custom serialization with Serializable too (thanks to the writeObject() method)

When you use Serializable, Java will automatically serialize all the fields of an object by default. You can customize this process using the writeObject() method, but it can still be limiting if you have complex objects or if you want to omit certain fields during serialization.

Externalizable gives you more control over how an object is serialized and deserialized. When you use Externalizable, you have to write your own serialization code using the writeExternal() and readExternal() methods. This lets you choose which fields to serialize and how to serialize them, so you can create more efficient or secure serialization code.

In general, you should use Serializable for simple objects where you don’t need a lot of control over the serialization process, and Externalizable for more complex objects where you need more control over how the object is serialized and deserialized.

I hope this helps!

Why can’t I write writeObject() the same way I would write writeExternal() (omitting fields etc.)?

When you use Serializable, Java automatically serializes all fields of an object by default, but you can customize this with the writeObject() method. Externalizable, on the other hand, gives you more control over how an object is serialized and deserialized using the writeExternal() and readExternal() methods. This can be useful for more complex objects where you need more control over the serialization process. While writeObject() can also omit fields during serialization, writeExternal() requires more manual implementation and gives you even greater control over versioning, security concerns, and object references. So, choose Serializable for simpler objects and Externalizable for more complex ones where greater control is needed.

even greater control over versioning, security concerns, and object references

What exactly can I do with Externalizable that I can’t do with Serializable and its writeObject()?