Java: Object Serialization with Inheritance Example


As we have read in the previous post if we implement java.io.Serializable interface on parent class then no need to implement on child class to make it serializable. Here we will discuss for both the cases after java.io.Serializable:

  • Implements Serializable on Parent Class
  • Implements Serializable on Child Class

Pre-requisite:

Example: Serializable interface implements on Parent class Only

Here you will see while the Serializable interface implemented on parent class but all the fields of parent and child class serialized.

import java.io.Serializable;

public class Person implements Serializable{
	private String name;
	private String citizenship;

	public Person()
	{
	}

	public Person(String name, String citizenship) {
		super();
		this.name = name;
		this.citizenship = citizenship;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCitizenship() {
		return citizenship;
	}

	public void setCitizenship(String citizenship) {
		this.citizenship = citizenship;
	}
}
public class Employee extends Person {
	private int employeeId;
	private String department;
	private int salary;

	public Employee() {

	}

	public Employee(int employeeId, String name, String department, String citizen,int salary) {
		super(name, citizen);
		this.employeeId = employeeId;
		this.department = department;
		this.salary=salary;
		System.out.println("Employee Constructor Executed.");
	}

	public int getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}

}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializationAndDeserializationInheritanceExample {

	public static void main(String[] args) throws Exception {
		// creating employee object
		Employee e1 = new Employee(111, "Saurabh","IT","India", 52000);

		//serialization of object
		serializeEmployee(e1, "employee.txt");

		//deserialization of object
		e1 = deserializeEmployee("employee.txt");
		System.out.println("Employee Detail :");
		System.out.println("Id :" + e1.getEmployeeId() + "\nName: " + e1.getName() + "\nDepartment: " + e1.getDepartment()+ "\nSalary: " + e1.getSalary()+ "\nCitizenship: " + e1.getCitizenship());
	}

	private static void serializeEmployee(Employee e1, String fileName) throws Exception {
		// writing object into employee file
		FileOutputStream f = new FileOutputStream(fileName);
		ObjectOutputStream out = new ObjectOutputStream(f);
		// Serialize employee object as stream and write in file
		out.writeObject(e1);
		out.flush();
		out.close();
		f.close();
		System.out.println("Employee object Serialize Successfully");
	}

	private static Employee deserializeEmployee(String fileName) throws Exception {
		// Read object stream from file
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
		//Deserialize object stream to employee object
		Employee e = (Employee) in.readObject();
		in.close();
		return e;
	}
}

Output


Employee Constructor Executed.
Employee object Serialize Successfully
Employee Detail :
Id :111
Name: Saurabh
Department: IT
Salary: 52000
Citizenship: India

Example: Serializable interface implements on Child class Only

Here you will notice parent class fields are not serialize.

public class Person {
	private String name;
	private String citizenship;

	public Person()
	{
	}

	public Person(String name, String citizenship) {
		super();
		this.name = name;
		this.citizenship = citizenship;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCitizenship() {
		return citizenship;
	}

	public void setCitizenship(String citizenship) {
		this.citizenship = citizenship;
	}
}
import java.io.Serializable;

public class Employee extends Person implements Serializable{
	private int employeeId;
	private String department;
	private int salary;

	public Employee() {

	}

	public Employee(int employeeId, String name, String department, String citizen,int salary) {
		super(name, citizen);
		this.employeeId = employeeId;
		this.department = department;
		this.salary=salary;
		System.out.println("Employee Constructor Executed.");
	}

	public int getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}

}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializationAndDeserializationInheritanceExample {

	public static void main(String[] args) throws Exception {
		// creating employee object
		Employee e1 = new Employee(111, "Saurabh","IT","India", 52000);

		//serialization of object
		serializeEmployee(e1, "employee.txt");

		//deserialization of object
		e1 = deserializeEmployee("employee.txt");
		System.out.println("Employee Detail :");
		System.out.println("Id :" + e1.getEmployeeId() + "\nName: " + e1.getName() + "\nDepartment: " + e1.getDepartment()+ "\nSalary: " + e1.getSalary()+ "\nCitizenship: " + e1.getCitizenship());
	}

	private static void serializeEmployee(Employee e1, String fileName) throws Exception {
		// writing object into employee file
		FileOutputStream f = new FileOutputStream(fileName);
		ObjectOutputStream out = new ObjectOutputStream(f);
		// Serialize employee object as stream and write in file
		out.writeObject(e1);
		out.flush();
		out.close();
		f.close();
		System.out.println("Employee object Serialize Successfully");
	}

	private static Employee deserializeEmployee(String fileName) throws Exception {
		// Read object stream from file
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
		//Deserialize object stream to employee object
		Employee e = (Employee) in.readObject();
		in.close();
		return e;
	}
}

Output


Employee Constructor Executed.
Employee object Serialize Successfully
Employee Detail :
Id :111
Name: null
Department: IT
Salary: 52000
Citizenship: null

See Also:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s