Serializable interface in java example

Serializable interface in java example

Ans : In Java, the Serializable interface is a marker interface that allows classes to be serialized. Serialization is the process of converting an object into a stream of bytes, which can be persisted to a file, sent over a network, or stored in a database. Here’s an explanation followed by a Java example:

Serializable interface

Benefits of Serializable Interface

1. Object Persistence

   Explanation: Serializable allows objects to be converted into a byte stream, enabling them to be stored in files or transmitted across networks. This is crucial for saving the state of an object and reconstructing it later.

2. Inter-process Communication

   Explanation: Serialized objects can be sent over network connections or between different processes, making Serializable essential for distributed systems and remote method invocation (RMI).

3. Integration with Java APIs

   Explanation: Many Java APIs and frameworks rely on serialization, such as Java Persistence API (JPA) for object-relational mapping (ORM), Java Remote Method Invocation (RMI), and Java Message Service (JMS).

Example in Java

Let’s demonstrate the use of Serializable with a simple Java example:

Example
java
import java.io.*;

// Example class implementing Serializable interface
class Person implements Serializable {
    private String name;
    private int age;
	
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getter and Setter methods

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Main {

    public static void main(String[] args) {
        // Creating an instance of Person
        Person person = new Person("Alice", 30);

        // Serialization
        try {
            FileOutputStream fileOut = new FileOutputStream("person.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(person);
            out.close();
            fileOut.close();
            System.out.println("Serialized data is saved in person.ser");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Deserialization
        try {
            FileInputStream fileIn = new FileInputStream("person.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            Person deserializedPerson = (Person) in.readObject();
            in.close();
            fileIn.close();
            System.out.println("Deserialized data:");
            System.out.println(deserializedPerson);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Explanation: In this example:

  • Person class implements Serializable, allowing instances of Person to be serialized and deserialized.
  • Serialization (ObjectOutputStream) writes the object (person) to a file (person.ser), converting its state into a byte stream.
  • Deserialization (ObjectInputStream) reads the byte stream from person.ser and reconstructs the Person object (deserializedPerson), demonstrating object persistence.

Homepage

Readmore