Master object serialization, Serializable interface, ObjectInputStream, ObjectOutputStream, deserialization, and serialization best practices for the OCP 21 exam.
Table of Contents
1. Serialization Overview
Serialization is the process of converting an object into a byte stream. Deserialization is the reverse process of reconstructing an object from a byte stream.
1.1 Use Cases
- Persistence - Save object state to file
- Network communication - Send objects over network
- Caching - Store objects in cache
- Deep copying - Clone objects
2. Serializable Interface
A class must implement Serializable interface to be serializable. It's a marker interface (no methods).
2.1 Implementing Serializable
Example:
import java.io.Serializable;
class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
private transient String password; // Not serialized
// Constructor, getters, setters
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// serialVersionUID: Version control for serialization
// transient: Field not included in serialization
3. ObjectOutputStream
ObjectOutputStream is used to serialize objects to a byte stream.
3.1 Serializing Objects
Example:
import java.io.*;
Person person = new Person("Alice", 30);
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("person.ser"))) {
oos.writeObject(person);
oos.flush();
}
// Serializing multiple objects
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("persons.ser"))) {
oos.writeObject(new Person("Alice", 30));
oos.writeObject(new Person("Bob", 25));
}
4. ObjectInputStream
ObjectInputStream is used to deserialize objects from a byte stream.
4.1 Deserializing Objects
Example:
import java.io.*;
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("person.ser"))) {
Person person = (Person) ois.readObject();
System.out.println(person.getName());
}
// Deserializing multiple objects
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("persons.ser"))) {
Person p1 = (Person) ois.readObject();
Person p2 = (Person) ois.readObject();
}
5. Serialization Control
5.1 Custom Serialization
Example:
import java.io.*;
class Person implements Serializable {
private String name;
private int age;
// Custom serialization
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
oos.writeObject(name.toUpperCase()); // Custom logic
}
// Custom deserialization
private void readObject(ObjectInputStream ois)
throws IOException, ClassNotFoundException {
ois.defaultReadObject();
name = ((String) ois.readObject()).toLowerCase(); // Custom logic
}
}
6. Exam Key Points
Critical Concepts for OCP 21 Exam:
- Serializable: Marker interface for serializable classes
- serialVersionUID: Version control for serialization
- transient: Field not included in serialization
- ObjectOutputStream: Serialize objects to byte stream
- ObjectInputStream: Deserialize objects from byte stream
- writeObject(): Custom serialization method
- readObject(): Custom deserialization method
- NotSerializableException: Thrown if class not serializable
- Static fields: Not serialized (belong to class, not instance)
- Inheritance: Parent must be serializable if child is serializable
0 Comments