In this article, you will learn about Object Serialization in Java Programming Language.
Serialization is the process of representing an object state into byte streams. An object in java can be serialized by implementing the interface java.io.serializable or its subinterface java.io.externalizable.
Serialization is synonymous to the term Marshalling wherein the object’s state and codebase is recorded. However, there lies a slight difference between the two. Serialization doesn’t record codebases while Marshalling records codebases as well.
The process of converting the byte streams back to the object state is known as Deserialization. This is similar to the term Demarshalling or Unmarshalling.
Apart from Java, the other object-oriented programming languages which support object serialization are Php, Python, Ruby, Objective-C, Smalltalk, Delphi, etc.
USES OF SERIALIZATION IN JAVA
- Serializing an object helps in storing the data into the databases.
- It is used to transfer data from one program to another program.
- It is used in RPC(Remote Procedure Call) mechanisms.
- It is used to detect the changes in the data which varies with time.
- It is used to distribute objects in CBD(Component-based Development) such as EJB(Enterprise JavaBeans) model, .NET model, COM(Component Object Model), CORBA(Common Object Request Broker Architecture) component model, XPCOM just to name a few.
OBJECT SERIALIZATION
To serialize an object state, ObjectOutputStream class is used which consists of following constructor and methods.
Constructor:
public ObjectOutputStream(OutputStream out) throws IOException { }
Methods:
- public final void writeObject(Object ob) throws IOException { }
- public void flush() throws IOException { }
- public void close() throws IOException { }
We have shown below an example of Object Serialization where the Trainer class implements the Serializable interface. An another class SerializationExample is used to instantiate and serialize the object into a file. After the successful execution of the program, a trainer.ser file is created.
Example:
import java.io.Serializable ;
class Trainer implements Serializable {
String trainername ;
int salary ;
public Trainer(String trainername, int salary) {
this.trainername = trainername ;
this.salary = salary ;
}
}
class SerializationExample {
public static void main(String args[])throws Exception{
Trainer t = new Trainer(“Amit Negi”, 40000) ;
FileOutputStream fileout1 = new FileOutputStream(“trainer.ser”) ;
ObjectOutputStream objectout2 = new ObjectOutputStream(fileout1) ;
objectout2.writeObject(t) ;
objectout2.close() ;
System.out.println(“This is an Example of Object Serialization”) ;
}
}
After the successful execution of the above program, the following output will be generated.
Output:
This is an Example of Object Serialization
OBJECT DESERIALIZATION
To deserialize the byte streams back to the object state, ObjectInputStream class is used which consist of the following constructor and methods.
Constructor:
public ObjectInputStream(InputStream in) throws IOException { }
Methods:
- public final Object readObject() throws IOException, ClassNotFoundException
- public void close() throws IOException { }
We have shown below an example of Object Deserialization where the readObject method deserializes the byte streams back to the object state.
Example:
class DeserializationExample {
public static void main(String args[])throws Exception {
ObjectInputStream objectin = new ObjectInputStream(new FileInputStream(“trainer.ser”)) ;
Trainer t = (Trainer)objectin.readObject() ;
System.out.println(t.trainername+” “+t.salary) ;
objectin.close() ;
}
}
After the successful execution of the above program, the following output will be generated.
Output:
Amit Negi 40000
OBJECT SERIALIZATION AND INHERITANCE
In java, when a class implements serializable interface, it means all the subclass of the parent class are also serializable. Let’s go through the below example to make it more clear.
Example:
import java.io.Serializable;
class Trainer implements Serializable {
String trainername ;
int salary ;
Trainer(String trainername, int salary) {
this.trainername = trainername ;
this.salary = salary ;
}
}
class W3TS extends Trainer {
String course ;
int coursefee ;
public W3TS(String trainername, int salary, String course, int coursefee) {
super(trainername, salary) ;
this.course = course ;
this.coursefee = coursefee ;
}
}
Also Read:- Generic Methods and Classes in Java Programming Language
We have provided you the description on Object Serialization and Deserialization in Java. Hope you like this post. For more updates and related information, stay connected to our blogs on Java Programming Language.