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 -

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