15

Although the java.io.Serializable interface public interface Serializable{} surprisingly doesn't contain any methods and fields in Java, the class that implements this interface is able to achieve the function of serialization and deserialization (the state of the object being serialized or deserialized). How can it achieve the function of serialization and deserialization without any methods or fields in Java?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Bhavesh
  • 4,607
  • 13
  • 43
  • 70

6 Answers6

14

Some interfaces serve only as "markers" or "flags".

The UID and custom readers/writers are accessed via reflection.

Serializable is a marker, and the JRE/JVM may take action(s) based on its presence.

http://en.wikipedia.org/wiki/Marker_interface_pattern

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
3

Serializable doesn't contain any method, it's the ObjectOutputStream and ObjectInputStream classes that can do that work, through the writeObject and readObject methods.

Serializable is just a marker interface, in other words it just puts a flag, without requiring any fields or methods.

stivlo
  • 83,644
  • 31
  • 142
  • 199
1

The Serializable interface is a marker interface which only notifies JVM that a certain object is set to be serialized. The serialization process happens internally.

Mechkov
  • 4,294
  • 1
  • 17
  • 25
0

I agree with irreputable, expecially the implicit assertion that every object should know how to serialize/deserialize itself, except his/her assertion that:

there is no way to provide default implementationsckquote

Sure there is: Object Composition.

George Aristy
  • 1,373
  • 15
  • 17
0

If you see the implementation of writeObject method of ObjectOutputStream, following snippet of code you will see inside the implementation.

Here you can see, if your class is not implementing Serializable interface, then NotSerializableException is thrown.

if (obj instanceof String) {
    writeString((String) obj, unshared);
} else if (cl.isArray()) {
    writeArray(obj, desc, unshared);
} else if (obj instanceof Enum) {
    writeEnum((Enum<?>) obj, desc, unshared);
} else if (obj instanceof Serializable) {
    writeOrdinaryObject(obj, desc, unshared);
} else {
    if (extendedDebugInfo) {
        throw new NotSerializableException(
            cl.getName() + "\n" + debugInfoStack.toString());
    } else {
        throw new NotSerializableException(cl.getName());
    }
}

Serializable is just a marker interface. Java internally use this to check that if object is actually writable or not.

nagendra547
  • 5,672
  • 3
  • 29
  • 43
0

A better design would have been

interface Serializable

    void writeObject(ObjectOutputStream out)

    void readObject(ObjectInputStream in)

The problem is, there is no way to provide default implementations; so every subclass must implement these two methods, which is a nuisance.

class MyClass implements Serializable

    // stupid boilerplate code
    void writeObject(ObjectOutputStream out)
    { 
        Util.defaultWriteObject(this, out);
    }  

In Java 8, this is going to change, we can have default impls for interface methods

interface Serializable

  void writeObject(ObjectOutputStream out) default Util::defaultWriteObject

  void readObject(ObjectInputStream in) default Util::defaultReadObject

(Serializable will not be changed, but the new feature can be used in comparable cases)

irreputable
  • 44,725
  • 9
  • 65
  • 93