Site icon

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

Impact of serialization on inheritance in java

1.) What happens, when concrete class is serialized?

2.) What happens, when concrete class is deserialized?

Fig 1: Employee implements Serializable

Code: Serialization when concrete class is serializable

1.) Person (Base) class (is not serializable)

package org.learn;

public class Person {

	public String firstName;
	public int age;
	public String contact;
	public Person(String firstName,int age, String contact) {
		this.firstName = firstName;
		this.age = age;
		this.contact = contact;
	}
	public Person() {
		
	}
	public String toString() {
	    return "[" + firstName +
		       " " + age + " " +contact +"]";
	}
}

2.) Employee (Concrete) class implements Serializable interface

package org.learn;

import java.io.Serializable;

public class Employee extends Person implements Serializable {

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

3.) SerializeDeserialize class

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, 9999, "Mike", 34, "001894536");
		//Set static variable
		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);
	} 
	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: Serialization in java, drive class is serializable

Serialized the employee object : [Mike 34 001894536][IT 9999 25]
Deserialize the employee object :[null 0 null][IT 9999 25]

Download Code : Inheritance & serialization in java

Exit mobile version