Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Serializable Interface in Java

  • Last Updated : 24 Nov, 2020

The Serializable interface is present in java.io package. It is a marker interface. A Marker Interface does not have any methods and fields. Thus classes implementing it do not have to implement any methods. Classes implement it if they want their instances to be Serialized or Deserialized. Serialization is a mechanism of converting the state of an object into a byte stream. Serialization is done using ObjectOutputStream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object. Deserialization is done using ObjectInputStream. Thus it can be used to make an eligible for saving its state into a file. 

Declaration

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

public interface Serializable

Example: The below example shows a class that implements Serializable Interface.



Java




// Java Program to demonstrate a class
// implementing Serializable interface
  
import java.io.Serializable;
  
public static class Student implements Serializable {
    public String name = null;
    public String dept = null;
    public int id = 0;
}

Here, you can see that Student class implements Serializable, but does not have any methods to implement from Serializable.

Example: Below example code explains Serializing and Deserializing an object.

Java




// Java program to illustrate Serializable interface
import java.io.*;
  
// By implementing Serializable interface
// we make sure that state of instances of class A
// can be saved in a file.
class A implements Serializable {
    int i;
    String s;
  
    // A class constructor
    public A(int i, String s)
    {
        this.i = i;
        this.s = s;
    }
}
  
public class Test {
    public static void main(String[] args)
        throws IOException, ClassNotFoundException
    {
        A a = new A(20, "GeeksForGeeks");
  
        // Serializing 'a'
        FileOutputStream fos
            = new FileOutputStream("xyz.txt");
        ObjectOutputStream oos
            = new ObjectOutputStream(fos);
        oos.writeObject(a);
  
        // De-serializing 'a'
        FileInputStream fis
            = new FileInputStream("xyz.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        A b = (A)ois.readObject(); // down-casting object
  
        System.out.println(b.i + " " + b.s);
  
        // closing streams
        oos.close();
        ois.close();
    }
}

Output:

20 GeeksForGeeks

Must Read: Serialization and Deserialization in Java




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!