Why default constructor is not invoked/called during deserialization mechanism?
Followed the discussion and comments but didn't find satisfactory answer.
I understand during the deserialization process
It creates the instance of Object class by invoking its default constructor and with the help of created instance, it creates instance of Parent and Child class using newConstructorForSerialization() method of ReflectionFactory class which internally create instance without invoking constructor of class.
What is the reason of implementing in this way?
public class EmployeeAddress/* implements Serializable */{
/**
*
*/
/*private static final long serialVersionUID = 4455988544392627515L;*/
protected String address;
public EmployeeAddress() {
System.out.println(this.address);
System.out.println("EmployeeAddress constructor has invoked with no-args");
}
public EmployeeAddress(String address) {
this.address = address;
System.out.println(this.address);
System.out.println("EmployeeAddress constructor has invoked with args");
}
// setter / getter
}
public class Employee extends EmployeeAddress implements Serializable{
/**
*
*/
/*public static final long serialVersionUID = 151736201136818635L;*/
private static final long serialVersionUID = 2L;
private String name;
private int age;
private String designation;
private double salary;
private PermanentEmployee pEmpoyee;
private transient long empId;
/*public Employee(String address) {super(address);}*/
public Employee(String name, int age, PermanentEmployee pEmpoyee, String designation, String address, long empId) {
super(address);
this.name = name;
this.age = age;
this.pEmpoyee = pEmpoyee;
this.designation = designation;
this.salary = 2000.00 + pEmpoyee.getBonus();
this.empId = empId;
}
// setter / getter
}
public class PermanentEmployee implements Serializable{
/**
*
*/
private static final long serialVersionUID = -6153718980149785271L;
private double bonus;
public PermanentEmployee() {}
public PermanentEmployee(double bonus) {
this.bonus = bonus;
}
// setter / getter
}
public class SerializationDemo {
public static void main(String[] args) {
PermanentEmployee pEmpoyee = new PermanentEmployee(1000.00);
com.serialization.first.Employee emp = new com.serialization.first.Employee("XYY", 30, pEmpoyee, "Leader", "Nagpur", 2001);
WriterAndReader.write(emp, "emp.ser");
System.out.println("Serailization processed..for com.serialization.first.Employee ");
com.serialization.first.Employee emp1 = (com.serialization.first.Employee) WriterAndReader.read("emp.ser");
System.out.println("Name: "+ emp1.getName()
+ ", Age: "+ emp1.getAge()
+ ", Designation: "+ emp1.getDesignation()
+ ", Salary: " + emp1.getSalary()
+ ", Address: " + emp1.getAddress()
+ ", EmpId: " + emp1.getEmpId());
System.out.println("Deserailization processed.. for com.serialization.first.Employee ");
}
}
Output:
null
EmployeeAddress constructor has invoked with no-args
Name: XYY, Age: 30, Designation: Leader, Salary: 3000.0, Address: null, EmpId: 0
Deserailization processed.. for com.serialization.first.Employee