What is the difference between Serializable and Externalizable in Java?

后端 未结 11 1028
轻奢々
轻奢々 2020-11-22 12:31

What is the difference between Serializable and Externalizable in Java?

11条回答
  •  抹茶落季
    2020-11-22 13:11

    Key differences between Serializable and Externalizable

    1. Marker interface: Serializable is marker interface without any methods. Externalizable interface contains two methods: writeExternal() and readExternal().
    2. Serialization process: Default Serialization process will be kicked-in for classes implementing Serializable interface. Programmer defined Serialization process will be kicked-in for classes implementing Externalizable interface.
    3. Maintenance: Incompatible changes may break serialisation.
    4. Backward Compatibility and Control: If you have to support multiple versions, you can have full control with Externalizable interface. You can support different versions of your object. If you implement Externalizable, it's your responsibility to serialize super class
    5. public No-arg constructor: Serializable uses reflection to construct object and does not require no arg constructor. But Externalizable demands public no-arg constructor.

    Refer to blog by Hitesh Garg for more details.

提交回复
热议问题