Georgi Georgiev
Georgi Georgiev

Reputation: 1623

Serialization not implemented?

What I understand is that I can implement Serializable interface to make my object serializable.

But I don't get where is writeObject method implemented when Serializable is an interface, so it doesn't contain implementation of methods, just a definition?

Upvotes: 3

Views: 1248

Answers (2)

Little Santi
Little Santi

Reputation: 8783

Tough all the answers posted so far are right, I wish to add some extra comments:

java.io.Serializable was already part of the Java 1.1 API (among the first versions of Java), and was meant as an easy way for the programmer to mark any class to have a special behaviour.

According to OOP principles, that should have been done through a regular interface, which is what you (and me, and any other programmer) would have expected. Something like this:

public interface Serializable<E>
{
    public E read(DataInput input) throws IOException;

    public void write(DataOutput output) throws IOException;
}

But, since there are many classes in Java which needed to be serialized, the Java Language designers wished to save troubles to programmers, by some kind of mechanism through which serialization would be performed automatically. But how?

  • Through an abstract class? Nope. That would have prevented any custom class to have its own hierarchy (since in Java there is only single inheritance).

  • Making java.lang.Object serializable? Neither so, because that would have prevented programmers to decide which class should be serializable and which should not.

On top of all, there was a hughe problem: Note that method read is supposed to create and return an object of class E from a DataInput stream. An abstract class just can not create instances of its subclasses whithout further information (the abstract class does not know which is the applied subclass).

So, they decided to pass over the OOP and offer Serialization as a special non-oop feature of the serialization classes ObjectOutputStream/ObjectInputStream (credits to EJP for this detail) in the form of a "dummy" interface recognizable by them, at the price of adding somehow some confussion to the class definitions, because an interface with no methods is nonsense (Same approach they adopted for java.lang.Cloneable).

Actually, it adds even more confussion, because custom serialization must be done by implementing private methods readObject and writeObject (as specified by ObjectOutputStream), which is a feature non describible in terms of a Java interface.

Nowadays, these kind of marking can be done through annotations. Well, think of Serializable as an interface which should have been an annotation, but still remains as an interface for those -endless- compatibility reasons.

Upvotes: 1

Grzegorz Piwowarek
Grzegorz Piwowarek

Reputation: 13773

As you already noticed, the Serializable is a Marker Interface and does not have any methods to implement. Implementing Serializable is just a note that this one is eligible for serialization which is handled using ObjectOutputStream.

Methods you mentioned need to be implemented in a class implementing the Serializable interface and will be picked up automatically. Since there is no obligation for implementing them, they are not included in the interface.

http://docs.oracle.com/javase/8/docs/platform/serialization/spec/serial-arch.html#a4539

Upvotes: 3

Related Questions