Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

The Serializable interface in Java is a marker interface (an interface with no methods) used to enable serialization of an object. Serialization is the process of converting an object’s state to a byte stream, thereby enabling the object to be easily saved to persistent storage or transmitted over a network. Deserialization is the reverse process, where the byte stream is converted back into a copy of the object.

Key Points about Serializable Interface

  1. Marker Interface: Serializable is a marker interface, meaning it has no methods to implement. Its purpose is to signal to the Java runtime that the class can be serialized.

  2. Default Serialization Mechanism: By implementing the Serializable interface, a class is indicating that it can be serialized using Java’s built-in serialization mechanism.

  3. Serialization and Deserialization Process: During serialization, the ObjectOutputStream is used to write the object’s state to an output stream. During deserialization, the ObjectInputStream is used to read the object’s state from an input stream.

  4. transient Keyword: Fields marked as transient are not serialized. This is useful for fields that contain sensitive information or fields that are derived from other fields and don’t need to be stored.

  5. serialVersionUID: This is a unique identifier for each class. It is used during deserialization to ensure that a loaded class corresponds exactly to a serialized object. If the class changes after the object was serialized, deserialization will fail unless serialVersionUID is defined explicitly and matches.

Example

Let’s look at a simple example to illustrate the use of the Serializable interface.

Defining a Serializable Class

				
					import java.io.Serializable;

public class Person implements Serializable {
    private static final long serialVersionUID = 1L; // Optional but recommended
    
    private String name;
    private int age;
    private transient String password; // This field will not be serialized

    public Person(String name, int age, String password) {
        this.name = name;
        this.age = age;
        this.password = password;
    }

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

    // Getters and setters...
}

				
			

Serializing an Object

				
					import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializeDemo {
    public static void main(String[] args) {
        Person person = new Person("John Doe", 30, "secretPassword");

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

				
			

Deserializing an Object

				
					import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class DeserializeDemo {
    public static void main(String[] args) {
        Person person = null;

        try (FileInputStream fileIn = new FileInputStream("person.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn)) {
            person = (Person) in.readObject();
        } catch (IOException | ClassNotFoundException i) {
            i.printStackTrace();
        }

        System.out.println("Deserialized Person...");
        System.out.println(person);
    }
}

				
			

Explanation

  1. Defining a Serializable Class: The Person class implements Serializable and has a serialVersionUID to ensure compatibility during deserialization. The password field is marked as transient and will not be serialized.

  2. Serializing an Object: The SerializeDemo class creates an instance of Person and writes it to a file named person.ser using ObjectOutputStream.

  3. Deserializing an Object: The DeserializeDemo class reads the person.ser file and reconstructs the Person object using ObjectInputStream. The password field will be null because it was marked as transient.

This example demonstrates the basic use of the Serializable interface to save and restore an object’s state.

Scroll to Top