Serialization and Deserialization in Java | Code Factory

Reference Link : Link

Donate : Link

Java Serialization allows us to convert Java Object to a Stream of bytes which we can send through a network or save in a file or even in a DB for future usage. Deserialization is the process of converting a stream of bytes back to Java Object which can be used in our program. We will be implementing java.io.Serializable interface to achieve serialization

The serializable interface in java is a marker interface (method with no body).

Employee.java

SerializableUtility.java

Main.java

Output :

Is sub class Serializable?

If the parent class is Serializable then all the sub class will be Serializable as well.

Person.java

Main.java

Output :

Other Class Reference in a Serializable class

If we have a non-serializable reference of a class inside a Serializable class, then serialization operation will not be performed.In such case, NonSerializableException will be thrown.

I have changed code of Employee.java and Person.java classes

Employee.java

Person.java

Main.java

Output :

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store