How many methods does Serializable have?
The Serializable interface in Java does not have any methods. It is a marker interface, which is an interface with no methods or fields. It is used to signal to the Java Virtual Machine (JVM) that the class implementing this interface can be serialized.

Table of Contents
Purpose of the Serializable Interface
The primary purpose of the Serializable interface is to enable the serialization and deserialization of objects. When a class implements this interface, it indicates that the class is eligible for the default serialization mechanism provided by the JVM. This allows objects of the class to be converted into a byte stream, which can then be written to a file or sent over a network, and later reconstructed into an object.
If a member (field) of a class doesn’t implement the interface, the serialization process will fail with a java.io.NotSerializableException. To handle such scenarios, you can use the transient keyword to mark fields that should not be serialized. Transient fields are ignored during serialization and are not included in the serialized byte stream.
Methods Used During Serialization and Deserialization Process
While the Serializable interface itself does not contain any methods, the serialization and deserialization processes in Java make use of the following methods from the ObjectOutputStream and ObjectInputStream classes:
1. Serialization
  ObjectOutputStream.writeObject(Object obj): Serializes the specified object and writes it to the ObjectOutputStream.
2. Deserialization
  ObjectInputStream.readObject(): Reads an object from the ObjectInputStream and deserializes it.
java
import java.io.Serializable;
public class Car implements Serializable {
private static final long serialVersionUID = 1L;
private String brand;
private String model;
private transient Engine engine; // Engine is not Serializable
public Car(String brand, String model, Engine engine) {
this.brand = brand;
this.model = model;
this.engine = engine;
}
@Override
public String toString() {
return "Car{brand='" + brand + "', model='" + model + "', engine=" + engine + "}";
}
}
class Engine {
private int horsepower;
public Engine(int horsepower) {
this.horsepower = horsepower;
}
@Override
public String toString() {
return "Engine{horsepower=" + horsepower + "}";
}
}
2. Serialization:
java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializeCar {
public static void main(String[] args) {
Engine engine = new Engine(200);
Car car = new Car("Toyota", "Camry", engine);
try (FileOutputStream fileOut = new FileOutputStream("car.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(car);
System.out.println("Serialized data is saved in car.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
}
3. Deserialization:
java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializeCar {
public static void main(String[] args) {
Car car = null;
try (FileInputStream fileIn = new FileInputStream("car.ser");
ObjectInputStream in = new ObjectInputStream(fileIn)) {
car = (Car) in.readObject();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Car class not found");
c.printStackTrace();
}
System.out.println("Deserialized Car:");
System.out.println(car);
}
}
Explanation of the Example
- 1. Serializable Class
- The Car class implements the Serializable interface.
- The Engine class does not implement , so it is marked as transient in the Car class.
- 2. Serialization
- The SerializeCar class creates a Car object and serializes it to a file named car.ser
- ObjectOutputStream.writeObject(Object obj) is used to serialize the Car object.
- 3. Deserialization
- The DeserializeCar class reads the Car object from the car.ser file using ObjectInputStream.readObject().
- The deserialized object is then printed to the console.
- Note that the engine field will be null after deserialization because it was marked as transient.