10

I know what is serialization and why it is used, but my question:

  1. Why is serialization a marker interface?
  2. What is the potential benefit of not having writeObject, readObject in Serializable interface because when we do serialization we finally override these 2 methods?
  3. How does readResolve ensure that the object created during deserialization is not the new object. I know the below and it is returning the same object during deserialization but who will call this readResolve method internally?

    private Object readResolve() throws java.io.ObjectStreamException { 
        return INSTANCE;
    }
    
gronostaj
  • 2,231
  • 2
  • 23
  • 43
Lathy
  • 879
  • 1
  • 8
  • 25
  • 3
    *because when we do serialization we finally override these 2 methods?*: no, you don't. *Who will call this readResolve method internally?*: the ObjectInputStream (or one of the classes it uses), which is used to deserialize objects. – JB Nizet Jul 14 '15 at 06:52
  • Interfaces cannot specify private methods, for one thing. – user253751 Jul 14 '15 at 09:15
  • @ gronostaj: I believe that link do not answer all my questions. – Lathy Jul 14 '15 at 10:53

2 Answers2

10
  1. Because there needs to be some explicit way of declaring a class to be serializable. The framework can't just assume all classes to be serializable, as there are many kinds of objects that will stop working if their fields are written to disk and are later reloaded from there (e.g. FileInputStream, which relies on an open operating system file handle which might no longer exist when the object is deserialized). The modern way of having such a declaration would be an annotation, but those didn't exist in Java at the time serialization was added.
  2. You won't necessarily need to override them - if the default behavior of the serializer is good enough; you don't need to do anything but implement Serializable.
  3. The serialization framework calls it when an object has been completely deserialized. At this time, the object is allowed to inspect its own contents, and if it decides that it should instead be represented by another instance, it can return that instance instead (if not, it returns this). Whatever comes out of this method is returned to the code that requested the deserialization. If a preexisting object was returned, the new object created by the deserializer will not be seen by anyone and will eventually be garbage collected.
Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • Hi Aasmund,Thank you for your comments. Can you please explain in detail about your point 1? – Lathy Jul 14 '15 at 11:38
  • @Lathy: What part of it? – Aasmund Eldhuset Jul 14 '15 at 16:55
  • "The framework can't just assume all classes to be serializable.." – Lathy Jul 15 '15 at 05:51
  • @Lathy: Some classes are structured in such a way that if you just (recursively) serialize the fields of an instance to disk and later read them back into a new instance (possibly in another program or in another execution of the same program), they might not work. For example, instances that rely on sharing data will break (if instance `a` and `b` both refer to some object `c`, if you serialize them separately and deserialize them again, they will now refer to two distinct copies of `c`). (Will continue.) – Aasmund Eldhuset Jul 15 '15 at 17:30
  • (Continued.) Also, instances that refer to ephemeral things such as operating system handles, e.g. `FileInputStream`, will most likely not work after deserialization because the handle they refer to no longer exists (if you close the original `FileInputStream`, Java's "connection" to the open file will also be closed, and the file handle inside the deserialized `FileInputStream` is no longer valid). – Aasmund Eldhuset Jul 15 '15 at 17:33
2

Marker Interfaces are used to tell JVM to perform specific tasks. they don't have any method. Serializable is also a marker interface.

Serialization is the process of flattening the objects. when you implement serializable interface in a class, it tells JVM to serialize its object i.e. it has to be converted into stream.

Vivek Mishra
  • 1,772
  • 1
  • 17
  • 37