Serialization in Inheritance
=====================================
Case 1:
- Even though child class does not implement Serializable interface, we can serialize child class object if parent class implements Serializable interface.
- Means, serializable nature is inheriting from parent to child, hence if parent is serializable, then by default every child is serializable.
Ex:
Note: Object class does not implements Serializable interface.
Case 2:
- Even though parent class does not implement Serializable we can serialize child class object, if child class implements Serializable interface.
- Means, to serialize child class object parent class need not be serializable.
- At the time of serialization, JVM will check is any variable inheriting from non-serializable parent or not.
- If any variable inheriting from non-serializable parent, then JVM ignore original value and save default value to the file.
- At the time of de-serialization JVM will check is any parent class is non serializable or not.
- If any parent class is non serializable then JVM will execute instance control flow in every non serializable parent and share its instance variable to the current object.
- While executing instance control flow of non serializable parent, JVM will always call no argument constructor.
Hence every non serializable class should compulsory contain no argument constructor. It may be default constructor generated by compiler or customized constructor provided by programmer. Otherwise we will get runtime exception: InvalidClassException.