views:

86

answers:

3

As we know Serializable is a Marker Interface(ie Interface without any methods).

So i was wondering how implementing this interface makes an object of the implementing class to be persisted, As except the name Serializable nothing is there for this interface.

And are there any other features we get by implementing this interface.

Can we create a similar markup interface which does a same work with different name?(this question is just to try.)

EDIT: Can I extend the Serializable interface. and will the extended interface will also have the same property.

+2  A: 

The Serializable interface, like you said, only defines the class as serializable. It has no effect.

It is up to the Java serialization mechanism to do handle the object based on whether it is marked as Serializable or not. Java architects could just as easily have defined an interface IFoo which each class that implements it is serializable.

Bottom line: it's just a name.

Yuval A
In terms of the language, it is just a name. In terms of the intended use, it involves much more. If you do it wrong a serialized object may be tempered and the class you read at the other side of the wire may introduce malicious code.
OscarRyz
+4  A: 

Sounds easy but is not.

This interface is used only to make sure the developer is aware and conscious of the consequences of serialization ( sending only the needed data to the wire and avoid sending useless information by marking them as transient )

Not using it, would possible throw tons of useless bytes to the wire, when they are not needed. An application that uses serialization spends most of its perceived execution time in transferring that data either to the disk, or through the net etc.

By forcing the developers to say "Yes, I want to do that" the hope is that at least they step a minute and say ( Am I to serialize this big array of data that can be calculated later? )

This not necessarily happens all the time.

And are there any other features we get by implementing this interface.

Nope, that's it.

Can we create a similar markup interface which does a same work with different name?(this question is just to try.)

No, this is the special one. You could try a custom serialization mechanism, but that's a different story.

There are many other things that you must be aware when using serialization, like security and flexibility. For that I would recommend you to read

Item 74: Implement Serializable judiciously On the Effective Java Book.

Here's how that item start:

...Because this is so easy to do, there is a common misconception that serialization requires little effort on the part of the programmer. The truth is far more complex. While the immediate cost to make a class serializable can be negligible, the long-term cost are often substantial.

OscarRyz
+1  A: 

By implementing Serializable interface you tell JVM that it can be persisted. It has no other implications. Btw, it is a marker interface, not a "mark up" interface.

fastcodejava
thanks fastcodejava. i will correct it.
GK
Not the JVM - the seralization mechanism.
Thorbjørn Ravn Andersen