-1

If I want to Serialize a java object, I just have to implement marker interface Serializable, which does not have any methods.

class Employee implements Serializable{
    private int id;
    private String name;

    /*
    private void writeObject(ObjectOutputStream out) throws IOException{
        System.out.println("in write object");
    }
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
        System.out.println("in read object");
    }
    */

}

My queries (EDIT):

  1. By implementing marker interface Serializable in my code, Java is serializing the my object. Which class is responsible for Serialization of java objects implementing Serializable interface ?

    What is the entry point for this process? Who calls ObjectOutputStream?

  2. Even without implementing Externalizable interface, private methods of my class : readObject and writeObject have been invoked. These have been commented in above code to enable default Java Serializaiton. Why is it allowed in first place without explicitly implementing Externalizable interface?

EDIT: To make my question clear, why did Java allow private readObject & writeObject methods instead of forcing up to use Externalizable implementation to customize Serialization process.

From Java docs:

The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods.

Are both mechanism not achieving the same result?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • 2
    This is possibly one of the most controversial design decisions in Java. The Java serialization API is Magic (TM), all of its internals are a JVM implementation detail. – Boris the Spider Dec 13 '15 at 16:19
  • Hi guillaume girod-vitouchkina. Even that question also did not provide insight on who initiates Serialization process. Everyone says it is eligible automatically. My second question was not also answered there. – Ravindra babu Dec 13 '15 at 16:21
  • @BoristheSpider It is all defined in the Object Serialization Specification. No magic. – user207421 Dec 13 '15 at 19:46

2 Answers2

2

The class responsible for serializing objects is java.io.ObjectOutputStream. The deserialization is done in the java.io.ObjectInputStream class.

The JavaDoc of the class ObjectOutputStream contains also this:

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

private void readObject(java.io.ObjectInputStream stream)
   throws IOException, ClassNotFoundException;
private void writeObject(java.io.ObjectOutputStream stream)
   throws IOException;
private void readObjectNoData()
   throws ObjectStreamException;

Here is some pseudocode that shows how ObjectOutputStream works. I've omitted many details and part of the work is done in ObjectStreamClass.

class ObjectOutputStream {

    writeObject(obj) {
        writeObject0(obj);  
    }

    writeObject0(obj) {
        if (hasWriteReplaceMethod(obj)) {
            obj = obj.writeReplace();
        }
        writeOrdinaryObject(obj);
    }

    writeOrdinaryObject(obj) {
        if (isExternalizable(obj)) {
            writeExternalData(obj);
        } else {
            writeSerialdata(obj);
        }
    }

    writeExternalData(obj) {
        obj.writeExternal(this);
    }

    writeSerialData(obj) {
        if hasWriteObjectMethod(obj)
            obj.writeObject(this);
        else
            defaultWriteFields(obj);
    }
}
Community
  • 1
  • 1
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • Your statement true for implementing Externalizable interface. I am looking for entry point of Serialization with marker interface Serializable without implementing those methods & how readObject & writeObject have been called without explicitly implementing that Externalizable interface. – Ravindra babu Dec 13 '15 at 16:45
  • 1
    Then I must have been doing something wrong all the time. Up until now I've serialized all my serializable classes with ObjectOutputStream.writeObject(), and none of them have implemented the Externalizable interface. – Thomas Kläger Dec 13 '15 at 17:11
  • Remove those methods and test the code by just implementing marker interface Serializable. Your class will be serialized with out implementing those two methods. Those methods have been provided for you to customize serializaiton. – Ravindra babu Dec 13 '15 at 17:14
  • @ravindra There is nothing in this answer that depends on `Externalizable`. The procedure in your second comment does not prove otherwise. – user207421 Dec 13 '15 at 19:24
  • I know that APIs are different in Externalizable. But why two mechanisms in place : one with clear specification and other with back door? Making things as explicit would be better. – Ravindra babu Dec 14 '15 at 01:40
  • @ravindra You're twenty years too late with that question, and asking the wrong people in the wrong place. It is what it is. – user207421 Dec 14 '15 at 01:44
1

Which class initiates Serialization process?

java.io.ObjectOutputStream.

  1. By implementing marker interface Serializable in my code, Java is serializing the my object. Which class is responsible for Serialization of java objects implementing Serializable interface?

java.io.ObjectOutputStream, again. You seem to be looking for something magical that isn't there.

  1. Even without implementing Externalizable interface, private methods of my class : readObject and writeObject have been invoked. These have been commented in above code to enable default Java Serializaiton. Why is it allowed in first place without explicitly implementing Externalizable interface?

The methods you name have nothing to do with the Externalizable interface, which contains two quite different methods. The answer to your question is simply that that is how it was designed, so as to allow a degree of custom Serialization without requiring the Externalizable interface, and it is implemented via Reflection.

why did Java allow private readObject & writeObject methods instead of forcing up to use Externalizable implementation to customize Serialization process.

They aren't equivalent, so the question isn't meaningful. At the Serializable level, Java provides for automatically handling local and parent non-transient state, whereas in Externalizable you have to do that entirely yourself.

Are both mechanism not achieving the same result?

No.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Still I feel some magic at Serialization process. I would like to know the caller (Entry point) of java.io.ObjectOutputStream, who is responsible for this process. – Ravindra babu Dec 14 '15 at 01:38
  • *Your code* is the caller, and the 'entry point' that you call is the `writeObject()` method. And, as I have already said, it's not magic, it is Reflection. – user207421 Dec 14 '15 at 01:42
  • Hmm. I am looking at some class which checks is this object Serializable or Externizable and calls respective APIs for serialization. – Ravindra babu Dec 14 '15 at 01:46
  • Looking *for* such a class, you mean? Same answer. `java.io.ObjectOutputStream`. Again, you keep looking for magic which simply isn't there. – user207421 Dec 14 '15 at 01:49
  • I am clear on this query now :) Still not convinced on other query of private methods when Externalizable in place. – Ravindra babu Dec 14 '15 at 01:52
  • What other query? I have directly addressed your edit above. If the class implements `Externalizable`, the private methods aren't called, but the methods of `Externalizable` are. But you didn't ask that. – user207421 Dec 14 '15 at 01:56