Example
Let's take a look at an example that shows how to use the Serializable interface to serialize and deserialize an object:
Java
mport java.io.*;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public static void main(String[] args) {
Person person = new Person("Rahul", 25);
// Serialization
try {
FileOutputStream fileOut = new FileOutputStream("/tmp/person.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(person);
out.close();
fileOut.close();
System.out.println("Serialized data is saved in /tmp/person.ser");
} catch (IOException e) {
System.err.println("Serialization failed: " + e.getMessage());
e.printStackTrace();
}
// Deserialization
Person deserializedPerson = null;
try {
FileInputStream fileIn = new FileInputStream("/tmp/person.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
deserializedPerson = (Person) in.readObject();
in.close();
fileIn.close();
} catch (IOException e) {
System.err.println("Deserialization failed: " + e.getMessage());
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.err.println("Person class not found: " + e.getMessage());
e.printStackTrace();
}
if (deserializedPerson != null) {
System.out.println("Deserialized Person object:");
System.out.println(deserializedPerson);
}
}
}

You can also try this code with Online Java Compiler
Run Code
In this example, we define a Person class that implements Serializable. The class has two fields: name (String) & age (int), both of which are serializable.
Inside the main method, we create an instance of the Person class & serialize it to a file named "person.ser" using FileOutputStream & ObjectOutputStream. We then deserialize the object from the file using FileInputStream & ObjectInputStream, casting the result back to a Person object.
Finally, we print out the deserialized Person object to verify that the serialization and deserialization process worked correctly.
Output
Serialized data is saved in person.ser
Deserialized Person object:
Person{name='Rahul', age=25}
This code example shows the complete usage of the Serializable interface in Java. The Person class implements Serializable, allowing its instances to be serialized and deserialized.
The main method performs the following steps:
- It creates an instance of the Person class with the name "Rahul" and age 25.
- It serializes the Person object and saves it to a file named "person.ser" using FileOutputStream and ObjectOutputStream.
- It deserializes the object from the "person.ser" file using FileInputStream and ObjectInputStream.
- Finally, it prints the deserialized Person object to the console to verify that the serialization and deserialization process worked correctly.
Frequently Asked Questions
What happens if a non-serializable class is inherited by a serializable class?
If a serializable class inherits from a non-serializable class, the non-serializable class must have a no-arg constructor to allow deserialization of the serializable subclass.
Can static fields be serialized?
No, static fields belong to the class itself rather than instances of the class, so they are not serialized.
What is the purpose of the serialVersionUID field?
The serialVersionUID is used to verify that the serialized and deserialized objects are compatible. If not specified, the JVM will generate one automatically, but it's recommended to declare it explicitly to avoid issues with different class versions.
Conclusion
In this article, we've learned about the Serializable interface in Java, which enables the serialization and deserialization of objects.
We explained how to declare a class as serializable, use transient fields for non-serializable data, and saw an example of serializing and deserializing a Person object.
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.