Serialization with default methods in interface (BWC)

liotur :

I have an interface A, and implementation of this interface AImpl

Interface has 1 method which is implemented in AImpl. Implementation AImpl has few members and no serialVersionUID.

Question what will happen If new default method will be added to interface A, without overriding it in AImpl ? Have I did break here? Will I have same result of serialization/deserialization ?

Peter Lawrey :

Not only are methods not serialized, but interfaces are not serialized either.

The purpose of serialization is to write the data associated with an object, not it's behaviour (unless this is explicitly encoded as data as well, e.g. GregorianCalendar's TimeZone)

There is no way to see in a serialized object what interfaces or methods the class has so changing them has no effect.

(You might infer about the methods used for serialization only)

Java serialization does encode the super-class hierarchy of a class so you can't change that easily.

However, most serialization libraries don't do this and you can't see this information nor does it matter if you change them. The downside is that many serialization libraries don't support having the same field name multiple times in your class hierarchy but Java serialization does.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104263&siteId=1