Hi there !! Im preparing for OCJP . In Which I came up with a doubt !!! According to the book , Serialization has no methods to implement !! I got a doubt here . The interface is not even implementing the method then what is the use of using the serializable Interface . I m thinking then for what purpose we are implementing the specially serializable interface.. Anyone give me the explaination about the marked interface and normal interface ?this quest may be silly but i dont think so rather i can ask here to get clear in JAVA
When a class implements serializable it informs the JVM that this class needs to be serializied. when a class is marked serializable it generate a serialUID for this class and compares when you desirialize the object.
It can also be called as Tagged Interface, Dummy Interface, Empty Interface.
We are implementing this Interface just for tagging purpose.
Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.
We are using this interface to backup the current state of object and reusing it later through deserialization. If we want to send a value to one place to another usually we will save the data in DB and then transfer it. Instead we are send the data through network as a Object and in another end we will deserialize it.
Another realtime example for this is, When we saving a particular game in PC a file will be saved. That file will save the current instances of the object. Later it will deserialize when you continue your game by loading that file.
Look at following image. After serializing ‘SerializationBox’ in the above sample code, I opened the output in a hex editor. You can use Notepad++ and hex plugin to open the serialized file.
Let us look at contents byte by byte and find out what they are. It starts with “ac ed”. It is is called STREAM_MAGIC. It is a magic number (java API guys says) that is written to the stream header. It denotes that is start of serialzed content.
Similarly every character has a meaning. Actually the serialized file is more bulkier than you would expect, as it has a huge header the meta information of the classes involved and finally the content. Object Serialization Stream Protocol have a look at chapter 6.4.2 Terminal Symbols and Constants. It gives you list of symbols and constants used in serialization.
Decrypting Serialized Java Object
In the image, I have underline a unit of information in a separate color for you to easily identify.
ac ed – STREAM_MAGIC – denotes start of serialzed content
00 05 – STREAM_VERSION – serialization version
73 – TC_OBJECT – new Object
72 – TC_CLASSDESC – new Class Descriptor
00 26 – length of the class name
63 6f 6d 2e 6a 61 76 61 70 61 70 65 72 73 2e 73 61 6d 70 6c 65 2e 53 65 72 69 61 6c 69 7a 61 74 69 6f 6e 42 6f 78 – class name
57 fc 83 ca 02 85 f0 18 – SerialVersionUID
02 – this object is serializable
00 01 – count of properties in the serialzed class – one property in our example
42 00 10 – private byte
73 65 72 69 61 6c 69 7a 61 62 6c 65 50 72 6f 70 78 70 – property name – serializableProp in our example
0a – 10 the value – This is the persisted value of the property in our sample
Interfaces with empty bodies are often used as markers to tag classes as having a certain property or behavior. Such interfaces are also called ability interfaces. Java APIs provide several examples of such marker interfaces: java.lang.Cloneable, java.io.Serializable, java.util.EventListener.