Site icon

Impact of Inheritance on serialization example in java (Base class is serializable)

Example: Base class implements Serializable interface

Fig 1: Serialization of Employee object

Program: Serialization when base class is Serializable.

1.) Base class implements Serializable interface

package org.learn;

import java.io.Serializable;

public class Person implements Serializable {

	private static final long serialVersionUID = 1L;
	public String firstName;
	public static String lastName="Static member will not be serialzed";
	public int age;
	public transient String contact="Trasient member will not be serialized";
	public Person(String firstName,int age, String contact) {
		this.firstName = firstName;
		this.age = age;
		this.contact = contact;
	}
	
	public String toString() {
	    return "[" + firstName + " " + lastName +
		       " " + age + " " +contact +"]";
	}
}

2.) Concrete class (Employee) extends Base class 

The employee class is inheriting Person class, so Employee class will become the Serializable.

package org.learn;

public class Employee extends Person {

	private static final long serialVersionUID = 1L;
	public String department;
	public static int employCode = 1010;
	public int salary;
	public transient String project="Trasient member will not be serialized";
	public Employee(String department,int salary, String project,
			String firstName,int age, String contact) {
		super(firstName,age, contact);
		this.department = department;
		this.salary = salary;
		this.project = project;		
	}
	public String toString() {
	    return  super.toString() + "[" + department + " " + employCode +
		       " " + salary + " " +project +"]";
	}
}

3.) SerializeDeserialize class to serialize concrete class

SerializeDeserialize class have couple of methods as follows.

  1. serialize method is used to serialize Employee object
  2. deserialize method is used to deserialize Employee object.
package org.learn;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeDeserialize {

	public static void serialize() throws IOException {
		Employee employee = new Employee(
                                        "IT",25, "server", "Mike", 34, "001894536");
		//Set static variable
		Employee.lastName = "harvey";		
		FileOutputStream output = new FileOutputStream(
                                                        new File("savePerson.txt"));
		ObjectOutputStream outputStream = new ObjectOutputStream(output);
		outputStream.writeObject(employee);
		output.close();
		outputStream.close();
		System.out.println("Serialized the Employee object : "+employee);
		
		//Update the static variable to confirm that its not serialized
		Employee.lastName = "Setting last name after serialization";
		Employee.employCode = 9999;
	} 
	public static void deSerialize() throws IOException, ClassNotFoundException {
		
		FileInputStream inputStream=new FileInputStream(
                                                    new File("savePerson.txt"));
		ObjectInputStream objectInputStream=new ObjectInputStream(inputStream);
		Employee employee = (Employee) objectInputStream.readObject();
		System.out.println("Deserialize the Employee object :"+ employee);
                objectInputStream.close();
		inputStream.close();
	}
	
	public static void main(String[] args) 
                               throws IOException, ClassNotFoundException {
		serialize();
		deSerialize();
	}
}

Output: Base class implements Serializable interface

Serialized the Employee object : [Mike harvey 34 001894536][IT 1010 25 server]
Deserialize the Employee object :[Mike Setting last name after serialization 34 null][IT 9999 25 null]

Download Code : Inheritance & serialization in java

 

Exit mobile version