Object graphs in serialization

Whenever we are serializing an object ,the set of all objects which are reachable form that object will be serialized automatically. This group of objects is nothing but Object Graph.
In object graph every object should be serializable if atleast one object is not serializable then we will get runtime execution saying NonSerializableExecution.

Example

import java.io.*;
class Dog implements Serializable
{
Cat c=new Cat();
}
class Cat implements Serializable
{
Rat r=new Rat();
}
class Rat implements Serializable
{
int j=20;
}
class SerializeDemo2
{
public static void main(String args[])throws Exception
{
Dog d1=new Dog();
FileOutputStream fos=new FileOutputStream("abc.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(d1);
FileInputStream fis=new FileInputStream("abc.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
Dog d2=(Dog) ois.readObject();
System.out.println(d2.c.r.j);
}
}

Output
20

In the above program whenever we are serializing Dog object automatically Cat and Rat objects got serialized because these are part of ObjectGraph of Dog.
Among Dog, Cat and Rat objects if at least one object is not serializable then we will get Runtime Exception saying NotSerializableExecution.

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment