Serialization of Objects

  • Eclipse: Oxygen
  • Java: 1.8

Serialization of Objects

It is the converting of Java object into byte streams and deserialization, as the name suggests, in a contrast to it. Stated differently, serialization is the conversion of a Java object into a static stream (sequence) of bytes which can then be saved to a database or transferred over a network.

Here we are casting the information using another kind of vehicle version id class. Whereas we are reading some other vehicle id class. When we are trying to cast the information there is some inconsistency, issues with the version id. Making use of serialization is it gives us an additional layer of consistency, so there is no inconsistency throughout the application and we are making use of the exact class for casting the information while reading the file.

Java code for serialization

In the following program, to serialize an object, we need to ensure that the class of the object implements the interface. Serializable is a marker interface; It does not declare any method and tells the serialization mechanism that the class can be serialized.

Now that we have made the class eligible for serialization, the next step is really to serialize the object. This is done by calling the writeObject () method of the class.

Object restoration occurs with the call to the obj.readObject () method.  readObject () can read any serializable object, a conversion to the correct type is required.

Contributed by: Poonam Tomar

Full stack Java Developer

   Buy me a coffee!



Download file

Leave a Reply

Notify of
Close Menu