Sobes.tech
Junior — Middle

What is the purpose of the Serializable interface in Java?

sobes.tech AI

Answer from AI

The Serializable interface in Java is used to indicate that objects of a class can be serialized — that is, converted into a byte stream for storage or transmission.

Purpose:

  • Allows saving the state of an object to a file, database, or transmitting over a network.
  • Used in remote method invocation (RMI), caching, and data transfer between activities in Android.

A class implementing Serializable is not required to implement any methods (it is a marker interface), but all fields of the object must be either serializable or marked as transient.

Example:

import java.io.Serializable;

public class User implements Serializable {
    private String name;
    private int age;

    // constructors, getters, setters
}