Making Child classes as Non Serializable in java

前端 未结 7 1245
無奈伤痛
無奈伤痛 2020-12-09 16:43

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

7条回答
  •  一生所求
    2020-12-09 17:25

    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

提交回复
热议问题