Which method is used to deserialize an object from a stream?

Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams. The reverse process of creating object from sequence of bytes is called deserialization.

A class must implement Serializable interface present in java.io package in order to serialize its object successfully. Serializable is a marker interface that adds serializable behaviour to the class implementing it.

Java provides Serializable API encapsulated under java.io package for serializing and deserializing objects which include,

  • java.io.serializable
  • java.io.Externalizable
  • ObjectInputStream
  • public final Object readObject() throws IOException,ClassNotFoundException
    0

Java Marker interface

Marker Interface is a special interface in Java without any field and method. Marker interface is used to inform compiler that the class implementing it has some special behaviour or meaning. Some example of Marker interface are,

  • java.io.serializable
  • public final Object readObject() throws IOException,ClassNotFoundException
    2
  • public final Object readObject() throws IOException,ClassNotFoundException
    3
  • public final Object readObject() throws IOException,ClassNotFoundException
    4

All these interfaces does not have any method and field. They only add special behavior to the classes implementing them. However marker interfaces have been deprecated since Java 5, they were replaced by Annotations. Annotations are used in place of Marker Interface that play the exact same role as marker interfaces did before.

To implement serialization and deserialization, Java provides two classes ObjectOutputStream and ObjectInputStream.

ObjectOutputStream class

It is used to write object states to the file. An object that implements java.io.Serializable interface can be written to strams. It provides various methods to perform serialization.

ObjectInputStream class

An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream.

public final Object readObject() throws IOException,ClassNotFoundException5 and public final Object readObject() throws IOException,ClassNotFoundException6 Methods

The

public final Object readObject() throws IOException,ClassNotFoundException
5 method of
public final Object readObject() throws IOException,ClassNotFoundException
0 class serializes an object and send it to the output stream.

public final void writeObject(object x) throws IOException

The

public final Object readObject() throws IOException,ClassNotFoundException
6 method of ObjectInputStream class references object out of stream and deserialize it.

public final Object readObject() throws IOException,ClassNotFoundException

while serializing if you do not want any field to be part of object state then declare it either static or transient based on your need and it will not be included during java serialization process.

serialization and deserialization of objects

Example: Serializing an Object in Java

In this example, we have a class that implements Serializable interface to make its object serialized.

import java.io.*;
class Studentinfo implements Serializable
{
    String name;
    int rid;
    static String contact;
    Studentinfo(String n, int r, String c)
    {
    this.name = n;
    this.rid = r;
    this.contact = c;
    }
}

class Demo
{
    public static void main(String[] args)
    {
        try
        {
            Studentinfo si = new Studentinfo("Abhi", 104, "110044");
            FileOutputStream fos = new FileOutputStream("student.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(si);
            oos.flush();
            oos.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}

Object of Studentinfo class is serialized using

public final Object readObject() throws IOException,ClassNotFoundException
5 method and written to
import java.io.*;
class Studentinfo implements Serializable
{
    String name;
    int rid;
    static String contact;
    Studentinfo(String n, int r, String c)
    {
    this.name = n;
    this.rid = r;
    this.contact = c;
    }
}

class Demo
{
    public static void main(String[] args)
    {
        try
        {
            Studentinfo si = new Studentinfo("Abhi", 104, "110044");
            FileOutputStream fos = new FileOutputStream("student.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(si);
            oos.flush();
            oos.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}
2 file.

Example : Deserialization of Object in Java

To deserialize the object, we are using ObjectInputStream class that will read the object from the specified file. See the below example.

import java.io.*;

class Studentinfo implements Serializable
{
    String name;
    int rid;
    static String contact;
    Studentinfo(String n, int r, String c)
    {
    this.name = n;
    this.rid = r;
    this.contact = c;
    }
}

class Demo
{
    public static void main(String[] args)
    {
        Studentinfo si=null ;
        try
        {
            FileInputStream fis = new FileInputStream("/filepath/student.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            si = (Studentinfo)ois.readObject();
        }
        catch (Exception e)
        {
            e.printStackTrace(); }
            System.out.println(si.name);
            System.out. println(si.rid);
            System.out.println(si.contact);
        }
}

Abhi 104 null

Contact field is null because,it was marked as static and as we have discussed earlier static fields does not get serialized.

NOTE: Static members are never serialized because they are connected to class not object of class.

import java.io.*; class Studentinfo implements Serializable { String name; int rid; static String contact; Studentinfo(String n, int r, String c) { this.name = n; this.rid = r; this.contact = c; } } class Demo { public static void main(String[] args) { try { Studentinfo si = new Studentinfo("Abhi", 104, "110044"); FileOutputStream fos = new FileOutputStream("student.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(si); oos.flush(); oos.close(); } catch (Exception e) { System.out.println(e); } } } 3 Keyword

While serializing an object, if we don't want certain data member of the object to be serialized we can mention it transient. transient keyword will prevent that data member from being serialized.

Which method is used to deserialize an object?

The ObjectInputStream class contains readObject() method for deserializing an object.

Which method is used for object deserialization in Python?

The Python pickle module is a better choice for serialization and deserialization of python objects. If you don't need a human-readable format or if you need to serialize custom objects then it is recommended to use the pickle module.

Is a process of extracting object stream from file stream?

1. Which of these is a process of extracting/removing the state of an object from a stream? Explanation: Deserialization is a process by which the data written in the stream can be extracted out from the stream.

What is deserialization used for?

Deserialization is the process of reconstructing a data structure or object from a series of bytes or a string in order to instantiate the object for consumption. This is the reverse process of serialization, i.e., converting a data structure or object into a series of bytes for storage or transmission across devices.