Results 1 to 4 of 4
  1. #1
    ajeeb is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default How does de-serialization makes constructor calls ?

    Hi,
    Please refer to the code below and the output generated:

    Java Code:
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    public class SerlDeSerl {
    	
    	public static void main(String[] args) throws Exception {
    		System.out.print("Writing");
    		Sub sub = new Sub();
    		ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("D:\\Test\\Test.serl"));
    		os.writeObject(sub);
    		System.out.print("Reading");
    		ObjectInputStream is = new ObjectInputStream(new FileInputStream("D:\\Test\\Test.serl"));
    		is.readObject();
    		}
    }
    
    class Super  {
    	Super() {
    		System.out.print("Super");
    	}
    }
    
    class Sub extends Super implements Serializable {
    	Sub() {
    		System.out.print("Sub");
    	}
    }
    The output is WritingSuperSubReadingSuper.

    During the de-serialization process only the constructor of Super is called and not the Sub even we are de-serializing the Sub object. Why is it so ?
    The behaviour (output) of the code changes further if we remove 'implements Serializable' from the Sub class definition and put it in Super class definition. In this case, the output is: WritingSuperSubReading.
    How does constructor calls happens during the de-serialization process ?

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I actually created my own test so I could see what was happening. I learned something.

    The something I learned is that the de-serialization process does *not* call either default constructor. That was a major surprise.

    Here's my code; it is similar to yours, but it contains a bit more of a test.
    Java Code:
    package test;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.Serializable;
    import static java.lang.System.out;
    
    public class SerializationTest {
    	public static void main(String[] args) {
    		final File file;
    		final ObjectOutputStream oos;
    		final ObjectInputStream ois;
    		MySub sub = null;
    		try {
    			file = new File("c:/temp/serialdata.txt");
    			file.createNewFile();
    			oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
    			sub = new MySub();
    			sub.sayHello();
    			sub.setName("JavaForums");
    			oos.writeObject(sub);
    			oos.close();
    			sub = null;
    			ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
    			sub = (MySub) ois.readObject();
    			ois.close();
    			sub.sayHello();
    		}
    		catch (Throwable t) {
    			t.printStackTrace();
    		}
    	}
    }
    
    class MySuper
        implements
        Serializable {
    	private static final long serialVersionUID = 1L;
    
    	MySuper() {
    		out.println("In MySuper()");
    	}
    }
    
    class MySub
        extends
        MySuper {
    	private static final long serialVersionUID = 2L;
    	private String _Name = "World";
    
    	MySub() {
    		out.println("In MySub()");
    	}
    
    	void sayHello() {
    		out.println("Hello, "
    		    + _Name
    		    + "!");
    	}
    
    	void setName(final String pName) {
    		_Name = pName;
    	}
    }
    Here's the output...

    In MySuper()
    In MySub()
    Hello, World!
    Hello, JavaForums!

    The constructors are only called when MySub is explicitly constructed, not when it is de-serialized. I added the sayHello() and setName() methods to ensure that serialization was actually doing something.

    Everything looks right....

    BTW, even when writing simple test code, be sure to handle your streams appropriately, as in closing them.

  3. #3
    ajeeb is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    Hi,
    You have gone with the classical way of the serialization process where the parent class is serializable and hence the child class is also serializable. It's evidient from your output that there is no constructor call. But if you have a look at my piece of code, I have only made the child class serializable (implements Serializable) and not the parent class. If you apply the same to your piece of code, i.e. make the child class serializable (implements Serialzable) and not the parent class, your output will be:

    In MySuper()
    In MySub()
    Hello, World!
    In MySuper()
    Hello, JavaForums!

    You can see that there is a call to the super class's constructor during the de-serialization process. Any ideas ?

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Interesting. I not an expert on serialization, so this is learn as I go.

    Since the information for MySub is stored in the data stream, it appears that there is no need to call its constructor. MySuper, on the other hand, has no information, so its default constructor must be called in order to initialize its values.

    Interestingly, adding a transient field in MySub does *not* cause either constructor to fire, leaving the transient field uninitialized.

Similar Threads

  1. What makes a good programmer?
    By cruxblack in forum Forum Lobby
    Replies: 35
    Last Post: 11-16-2008, 04:22 AM
  2. Never makes it to the method!
    By minihazard10 in forum New To Java
    Replies: 9
    Last Post: 11-10-2008, 07:30 AM
  3. Replies: 0
    Last Post: 04-04-2008, 02:47 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •