English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In serialization, when inheritance is introduced, certain conditions are defined by the superclass and subclass, which makes it easier to understand the serialization in each case. The basic rules to follow are as follows.
1When the superclass is implemented, the serializable interface is available while the subclass is not.
In this case, even if the subclass does not implement the Serializable interface, by default, the objects of the subclass will also be serialized when the superclass is serialized.
public class TestSerialization { public static void main(String[] args) throws IOException, ClassNotFoundException { B obj = new B(); FileOutputStream fos = new FileOutputStream("abc.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(obj); FileInputStream fis = new FileInputStream("abc.ser"); ObjectInputStream ois = new ObjectInputStream(fis); B obj1 = (B)ois.readObject(); System.out.println(obj)1.i + "" + obj1.j); } } class A implements Serializable { int i = 10; } class B extends A { int j =20; }
Output Result
value of i is: 10 & value of j is: 20
2. When the superclass does not implement the serializable interface while the subclass does.
In this case, the instance variables of the superclass inherited in the subclass will not be serialized, and their allocated values will not be released during the serialization process of the subclass. Additionally, the JVM will also reallocate default initialization values to these superclass instance variables during the subclass serialization process. One point to note in this scenario is that the superclass must have a default no-argument constructor, because the JVM accesses the superclass during deserialization. If this constructor does not exist, a compile-time exception will occur.
public class TestSerialization { public static void main(String[] args) throws IOException, ClassNotFoundException { B obj = new B(10,20); FileOutputStream fos = new FileOutputStream("abcd.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(obj); FileInputStream fis = new FileInputStream("abcd.ser"); ObjectInputStream ois = new ObjectInputStream(fis); B obj1 = (B) ois.readObject(); System.out.println("value of i is: " +obj1.i + " & value of j is: " + obj1.j); } } class A { int i; A() { System.out.println("The default constructor of the superclass."); } A(int i) { this.i = i; } } class B extends A implements Serializable { int j; B(int i, int j) { super(i); this.j = j; } }
When there is a default constructor.
The default constructor of the superclass. value of i is: 0 & value of j is: 20
When there is no default constructor.
Exception in thread "main" java.io.InvalidClassException: B; B; no valid constructor
3. When it is necessary to serialize the superclass instead of the subclass (custom serialization).
To prevent subclasses from being serialized, we need to implementwriteObject()
andreadObject()
Methods executed by JVM during serialization and deserialization, as well as the NotSerializableException thrown by these methods. We can also provide custom logic in these methods, which will be executed during serialization/Exfoliating Agent.
public class TestSerialization { public static void main(String[] args) throws IOException, ClassNotFoundException { B obj = new B(); FileOutputStream fos = new FileOutputStream("abc.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(obj); FileInputStream fis = new FileInputStream("abc.ser"); ObjectInputStream ois = new ObjectInputStream(fis); B obj1 = (B)ois.readObject(); System.out.println("value of i is: " + obj1.i + " & value of j is: " + obj1.j); } } class A implements Serializable { int i = 10; } class B extends A { int j =20; } //Implement the writeObject method, private void writeObject(ObjectOutputStream out) throws IOException { throw new NotSerializableException(); } //Implement the readObject method, private void readObject(ObjectInputStream in) throws IOException { throw new NotSerializableException(); }
Output Result
Exception in thread "main" java.io.NotSerializableException at B.writeObject(A.java:20)