Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 11

Object Serialization and

Deserialization in OOP
using Java
Object Serialization
• To read an entire object from or write an entire object to a file, Java
provides object serialization.
• 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 serialized object is represented as a sequence of bytes that includes
the object’s data and its type information.
• After a serialized object has been written into a file, it can be read from
the file and deserialized to recreate the object in memory.
Object Serialization (Cont’d)
• 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 behavior 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 and ObjectOutputStream, e.t.c.
• 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 behavior or meaning. Some examples of
marker interfaces are, java.io.serializable, java.lang.Cloneable, and
java.util.RandomAccess.
Object Serialization (Cont’d)
• Since object serialization makes exact copies of objects, it
makes it possible to clone objects without overriding the clone
method of Object class.
Object Serialization (cont.)

• Classes ObjectInputStream and ObjectOutputStream,


which respectively implement the ObjectInput and
ObjectOutput interfaces, enable entire objects to be read from or
written to a stream.
• To use serialization with files, initialize ObjectInputStream and
ObjectOutputStream objects with FileInputStream and
FileOutputStream objects.
Object Serialization (cont.)

• ObjectOutput interface method writeObject() takes an Object as


an argument and writes its information to an OutputStream.
• A class that implements interface ObjectOuput (such as
ObjectOutputStream) declares this method and ensures that the object
being output implements Serializable.
• ObjectInput interface method readObject() reads and returns a
reference to an Object from an InputStream.
 After an object has been read, its reference can be cast to the object’s actual type.
Creating a Sequential-Access File Using Object
Serialization
• Objects of classes that implement interface Serializable can be
serialized and deserialized with ObjectOutputStreams and
ObjectInputStreams.
• Interface Serializable is a marker(or tagging) interface.
 It does not contain methods.
• A class that implements Serializable is tagged as being a
Serializable object.
• An ObjectOutputStream will not output an object unless it is a
Serializable object.
Creating a Sequential-Access File Using Object
Serialization (cont.)
• In a class that implements Serializable, every variable must be
Serializable.
• Any one that is not must be declared transient so it will be
ignored during the serialization process.
• All primitive-type variables are serializable.
• For reference-type variables, check the class’s documentation (and
possibly its superclasses) to ensure that the type is Serializable.
import java.io.*;
public class Studentinfo implements Serializable
{
String name;
int rid;
String contact;
Studentinfo(String n, int r, String c)
{
this.name = n;
this.rid = r;
this.contact = c;
}
}

public class Test


{
public static void main(String[] args)
{
try
{
Studentinfo si = new Studentinfo("Mukisa", 123, "0789123456");
FileOutputStream fos = new FileOutputStream("student2.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(si);
oos.close();
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
} }}
Reading and Deserializing Data from a Sequential-
Access File
• ObjectInputStream’s method readObject() reads an
Object from a .ser file.
• Method readObject() throws an EOFException if an attempt is
made to read beyond the end of the file.
• Method readObject() throws a ClassNotFoundException
if the class for the object being read cannot be located.
import java.io.*;
class DeserializationTest
{
public static void main(String[] args)
{
Studentinfo si=null;
try
{
FileInputStream fis = new FileInputStream("student2.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
si = (Studentinfo)ois.readObject();
ois.close();
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println(si.name);
System.out. println(si.rid);
System.out.println(si.contact);
}
}

You might also like