Bhavesh
Bhavesh

Reputation: 4677

Although the Serializable interface in Java has no methods, no fields, it can achieve its function. How?

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?

Upvotes: 17

Views: 18008

Answers (6)

nagendra547
nagendra547

Reputation: 6302

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.

Upvotes: 0

George Aristy
George Aristy

Reputation: 1481

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.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160170

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

Upvotes: 14

stivlo
stivlo

Reputation: 85476

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.

Upvotes: 3

irreputable
irreputable

Reputation: 45433

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)

Upvotes: 0

Mechkov
Mechkov

Reputation: 4324

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.

Upvotes: 1

Related Questions