GuruKulki GuruKulki -4 years ago 96
Java Question

Making Child classes as Non Serializable in java

I have a class which implements Serializable. Now I extends this class and I want this extended class to be non Serializable. So how to do it?

For example. I have

class A implements Serializable.


and I have

class B extends A.


But I want class B to be Non Serializable.

Answer Source

As others have made clear, it's not possible for a subclass of a Serializable class to be non-Serializable.

If what you want is for the subclass' attributes not to be serialized, one option is to make them all transient.

If you need more than that (you don't want super class fields to be serialized), override writeObject(ObjectOutputStream) and readObject(ObjectInputStream) as outlined here - https://web.archive.org/web/20120626144013/http://java.sun.com/developer/technicalArticles/ALT/serialization

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download