How can I serialize an interface?

后端 未结 3 1633
后悔当初
后悔当初 2021-01-02 03:53

Suppose I have a Serializable class ShapeHolder that owns an object that implements a Serializable Shape interface. I wa

3条回答
  •  失恋的感觉
    2021-01-02 04:38

    I want to make sure the correct concrete shape object is saved (and the correct type is later restored).

    Java's default serialization (ObjectInputStream & ObjectOutputStream) does that out-of-the-box. When serializing, Java writes there the name of the concrete class and then uses in when deserializing.

提交回复
热议问题