❮ Previous Next ❯

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.


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);
FileInputStream fis=new FileInputStream("abc.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
Dog d2=(Dog) ois.readObject();

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.

❮ Previous Next ❯


Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Email: info@silantechnology.com
      Phone: 0674-2361252
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved