Java I/O: Nested Objects Serialization

A Serializable object may contain references to other objects. We may be wondering if we serialize an object, whether the nested objects are also serialized. The answer is yes—the nested objects will also be serialized (unless they are marked transient) along with the top-level object, provided that all the concerned objects implement the Serializable or Externalizable interface. Here we provides an example to illustrate this feature.

Program Code

import java.awt.Color;



public class NestedObjectsApp {

                     public static void main(String args[]) {

                             Line line = new Line();

                             System.out.println("Before saving object:\n" + line);

                             try (ObjectOutputStream outStream = new ObjectOutputStream(

                                                new FileOutputStream("graph.dat"))) {


                             } catch (IOException ex) {

                                      System.out.println("Error writing object");


                             try (ObjectInputStream inStream = new ObjectInputStream(

                                                new FileInputStream("graph.dat"));) {

                                      line = (Line) inStream.readObject();

                             } catch (IOException ioe) {

                                      System.out.println("Error reading object");

                             } catch (ClassNotFoundException cfe) {

                                      System.out.println("Casting error");


                             System.out.println("\nAfter retrieving object:\n" + line);




class Point implements Serializable {

                     protected int x;

                     protected int y;


                     public Point(int x, int y) {

                             this.x = x;

                             this.y = y;




class ColorPoint extends Point implements Serializable {

                     private Color color;


                     public ColorPoint(int x, int y, Color color) {

                             super(x, y);

                             this.color = color;




                     public String toString() {

                             return "Point{" + "x=" + x + " y=" + y + '}' + " ColorPoint{"

                                                + "color=" + color + '}';




class Line implements Serializable {

                     private ColorPoint startPoint = new ColorPoint(0, 0,;

                     private ColorPoint endPoint = new ColorPoint(10, 10,;



                     public String toString() {

                             return "StartPoint=" + startPoint + "\nEndPoint=" + endPoint;





·         The program declares a class called Point having fields x, y. The class is serializable.

·         The ColorPoint class inherits the Point class and declares a Color field. This, too, is serializable.

·         The Line class declares two fields of the ColorPoint type and initializes them.

·     The main function creates a Line object and prints its initial state. It then serializes the created object to a disk file and closes it.

·        The program reopens the file, reads the data into a Line object, and prints its state.

·         When we run the program, we will notice that the original state of the Line object is restored.

·      It indicates that when we save an object, all its nested objects are also saved along with their inherent states.


The program output is given here:

Before saving object:

StartPoint=Point{x=0 y=0} ColorPoint{color=java.awt.Color[r=255,g=0,b=0]}

EndPoint=Point{x=10 y=10} ColorPoint{color=java.awt.Color[r=0,g=0,b=255]}


After retrieving object:

StartPoint=Point{x=0 y=0} ColorPoint{color=java.awt.Color[r=255,g=0,b=0]}

EndPoint=Point{x=10 y=10} ColorPoint{color=java.awt.Color[r=0,g=0,b=255]}


  Modified On Nov-21-2017 07:33:12 AM

Leave Comment