The Serializable Marker Interface
You'll learn what makes Serializable special and how it signals that a class can be converted to bytes.
In Java, most interfaces define methods that implementing classes must provide. But Serializable is different—it's a marker interface, meaning it contains absolutely no methods. Its sole purpose is to signal to the JVM: "This class is safe to serialize."
When you declare a class as Serializable, you're essentially giving Java permission to convert instances of that class into a stream of bytes. Without this marker, attempting to serialize an object will throw a NotSerializableException.
import java.io.Serializable;
public class User implements Serializable {
private String username;
private int age;
public User(String username, int age) {
this.username = username;
this.age = age;
}
}
Notice that Serializable doesn't force you to implement any methods—the interface is completely empty. It's like putting a sticker on your class that says "serialization-approved."
Why a Marker Instead of Methods?
Think of it like a driver's license. The license itself doesn't teach you to drive; it just proves you're authorized to do so. Similarly, Serializable doesn't define how to serialize—Java's serialization mechanism handles that automatically. The marker simply indicates the class author has considered serialization and consents to it.
What Happens Behind the Scenes
When Java sees the Serializable marker, it uses reflection to automatically discover all the object's fields and writes them to the byte stream. No manual work required from you—just add implements Serializable and you're done.
Key Takeaway: Serializable is a marker interface with no methods that tells Java a class can be safely serialized; it's a permission flag, not a contract requiring method implementations.