Hi,
Java Serialization takes care of saving that object's entire "object graph"
This means the object we are trying to serialize can have reference variables pointing to more objects.And all these objects will be serialized.Like if we serialize a Dog object, the Collar will be serialized automatically.
But this will only happen if all the objects have Serializable interface implemented to their classes.If this is not the case then NotSerializableException will be thrown.
NotSerializableException is occurring in the example where Collar class is not implementing the Serializable interface.
See in above code class B implements Serializable interface but its parent class A is not , so non-serializable class A constructor will run during deserialization.
I hope it helps