VOOZH about

URL: https://www.geeksforgeeks.org/java/object-serialization-inheritance-java/

⇱ Object Serialization with Inheritance in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Object Serialization with Inheritance in Java

Last Updated : 6 Nov, 2025

Object Serialization in Java allows you to save (serialize) and restore (deserialize) the state of an object, even when it’s part of an inheritance hierarchy. When dealing with inheritance, the serialization behavior depends on whether the superclass and subclass implement the Serializable interface.

  • Serialization: It is a mechanism of converting the state of an object into a byte stream. The byte array can be the class, version, and internal state of the object.
  • Deserialization: It is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.

Understanding Serialization with Inheritance

Serialization behavior changes depending on whether the superclass or subclass implements the Serializable interface. There are three main cases to understand:

Case 1: Superclass is Serializable -> Subclass is Automatically Serializable

If the superclass implements Serializable, then its subclasses are automatically serializable, even if they don’t explicitly implement the interface.

👁 case_1
Serialization with Inheritance

Output: 

👁 output
output

Explanation: Since class A implements Serializable, the subclass B is also serializable automatically. Both i and j retain their values after deserialization.

Case 2: If a superclass is not serializable, then subclass can still be serialized

Even if the superclass doesn’t implement Serializable, a subclass can still be serialized if it implements the Serializable interface. However, instance variables from the non-serializable superclass are not saved during serialization

Note: The non-serializable superclass must have a no-argument constructor, because it will be called during deserialization to reinitialize its part of the object.

👁 case_2
Serialization with Inheritance

Output
i = 10
j = 20
Object has been serialized
A's class constructor called
Object has been deserialized
i = 50
j = 20

Explanation: A is not serializable, so its field i is not saved. During deserialization, A’s default constructor is invoked, setting i = 50. The field j (from B) is restored correctly.

Case 3: If the superclass is serializable, but we don’t want the subclass to be serialized

If the superclass implements Serializable, there’s no direct way to stop a subclass from being serialized. However, we can manually block serialization by overriding the following methods in the subclass:

  • writeObject(ObjectOutputStream out)
  • readObject(ObjectInputStream in)

Throw a NotSerializableException inside these methods to prevent serialization and deserialization.

Output: 

👁 output
Output

Explanation: Here, the subclass B overrides writeObject() and readObject() to block serialization. This approach effectively prevents subclass objects from being serialized or deserialized, even though their superclass is serializable.

Comment
Article Tags:
Article Tags: