অবজেক্ট গ্রাফ হলো একে অপরের রেফারেন্স ধারণকারী অবজেক্টের সমষ্টি। যখন একটি অবজেক্ট সিরিয়ালাইজ করা হয়, তখন তার রেফারেন্সহোল্ড করা সব অবজেক্টও রিকার্সিভলি সিরিয়ালাইজ হয় (যদি সেগুলো Serializable ইমপ্লিমেন্ট করে)।
উদাহরণ
import java.io.*;
class One implements Serializable {
Two two = new Two();
}
class Two implements Serializable {
Three three = new Three();
}
class Three implements Serializable {
int i = 34;
int j = 67;
}
public class ObjectGraphDemo {
public static void main(String[] args) throws Exception {
One root = new One();
// সিরিয়ালাইজ
try (FileOutputStream fos = new FileOutputStream("graph.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(root);
}
// ডিসিরিয়ালাইজ
try (FileInputStream fis = new FileInputStream("graph.ser");
ObjectInputStream ois = new ObjectInputStream(fis)) {
One restored = (One) ois.readObject();
System.out.println("i = " + restored.two.three.i); // 34
System.out.println("j = " + restored.two.three.j); // 67
}
}
}
এখানে One সিরিয়ালাইজ করলে অটোমেটিক্যালি Two এবং Three ও সিরিয়ালাইজ হয়ে যায়। এটাকেই অবজেক্ট গ্রাফ বলে।