Saturday, April 21, 2012

JAVA.IO.SERIALIZATION: Custom Serialization Process (writeObject,readObject,writeReplace,readResolve)

JAVA.IO.SERIALIZATION:
>>>> USE OF "private void writeObject(ObjectOutputStream objOut)throws IOException{}",

>>>> USE OF "private void readObject(ObjectInputStream objIn)throws IOException{}",

>>>> USE OF "ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException{}";

>>>> USE OF "ANY-ACCESS-MODIFIER Object readResolve() throws ObjectStreamException{}";

Note: when we use writeReplace(..) method to replace the serializing object and serialize some other object then we must implement the readResolve(..) method in other object.
See example below, writeReplace() in Employee class, and readResolve() in Organization class.
################################################################################################

package io_serialization;

import java.io.Serializable;

public class Employee implements Serializable{

private static final long serialVersionUID = 1L;
int empId;
public Employee() {
super();
System.out.println("Employee instantialted..");
}

}

***********************************************************************
package io_serialization;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;

public class Engineer extends Employee{
private static final long serialVersionUID = 1L;
int age;
String name;
String address;
transient int salary=0;
//Organization org=null;

public Engineer(int age, String name, String address,String orgName,int orgId) {
super();
this.age = age;
this.name = name;
this.address = address;
this.salary=22000;
empId=101;
//org=new Organization(orgId,orgName);
System.out.println("Engineer object instantiated_param-constructor");
}

public Engineer(Organization o) {
System.out.println("Engineer object instantiated_param-constructor_org");
System.out.println("OrgData: "+o.orgId+" | "+o.orgName);
}

public String toString(){
return "Emp: "+name+" | "+age+" | "+address+" |transientVar "+salary+" |empId "+empId;
}

Engineer() {
super();
System.out.println("Engineer object instantiated_def-constructor");
}

/*private void writeObject(ObjectOutputStream objOut)throws IOException{
objOut.defaultWriteObject();
objOut.writeInt(salary);
}

private void readObject (ObjectInputStream objIn)throws IOException, ClassNotFoundException{
objIn.defaultReadObject();
salary=objIn.readInt();
}*/

public Object writeReplace()throws ObjectStreamException{
Organization org=new Organization(102,"abcd");
return org;
}

/*public Object readResolve()throws ObjectStreamException{
return new Employee();
}*/
}
**************************************************************************************

package io_serialization;

import java.io.Serializable;

public class Organization implements Serializable {
private static final long serialVersionUID = 1L;
int orgId;
String orgName="India pvt.ltd.";
public Organization() {
super();
System.out.println("Org instantiated.._default");
}

public Organization(int orgId, String orgName) {
super();
this.orgId = orgId;
this.orgName = orgName;
System.out.println("Org instantiated.._param");

}

public String toString(){
return "OrgData: "+orgId+" | "+orgName;
}

public Object readResolve(){
return new Engineer(this);
}
}
**************************************************************************************
package io_serialization;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializeObject {
public static void main(String[] args) {
ObjectOutputStream objOut=null;
try{
Engineer e=new Engineer(25,"abhinav","noida","BCDS",102);
System.out.println("Emp obj before serialization: "+e);
objOut=new ObjectOutputStream(new FileOutputStream("emp.txt"));
objOut.writeObject(e);
objOut.flush();
System.out.println("Emp object serialized..");
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(objOut!=null){
objOut.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
**************************************************************************************
package io_serialization;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class DeserializeObject {
public static void main(String[] args) {
ObjectInputStream objIn=null;
try{
Engineer e=null;
objIn=new ObjectInputStream(new FileInputStream("emp.txt"));
e=(Engineer)objIn.readObject();
System.out.println("Deserialized emp obj: "+e);
}catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if(objIn!=null){
try {
objIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
*********************************************************************************
OUTPUT SERIALIZATION>>>


Employee instantiated..
Engineer object instantiated_param-constructor
Emp obj before serialization: Emp: abhinav | 25 | noida |transientVar 22000 |empId 101
Org instantiated.._param
Emp object serialized..

OUTPUT DE-SERIALIZATION>>>


Employee instantiated..
Engineer object instantiated_param-constructor_org
OrgData: 102 | abcd
Deserialized emp obj: Emp: null | 0 | null |transientVar 0 |empId 0

Saturday, April 14, 2012

Does return type matters in case or overriding ???? Yes OR No?

Ques: Does return type matters in case or overriding ???? Yes OR No?
Ans: It matters ,but not in case of Inheritance

/**
* The Class NatureOfReturnTypeWhileOverriding.
*
* @author Abhinav kr.mishra
*/
public class NatureOfReturnTypeWhileOverriding {

/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {

Super superObj=new Super();
superObj.noReturn();
superObj.returnInt(12);
superObj.returnInteger();
superObj.returnNumber();
superObj.returnObject();
Sub subObj=new Sub();
subObj.noReturn();
subObj.returnInt(12);
subObj.returnInteger();
subObj.returnNumber();
subObj.returnObject();

}

}

class Super{

public Super() {
super();
System.out.println("Super instantiated..");
}

public void noReturn(){
System.out.println("super.noReturn");
}

public int returnInt(int x){
System.out.println("super.returnInt(..) : "+x);
return x;
}

public Integer returnInteger(){
System.out.println("super.returnInteger(..)");
return new Integer(1);
}

public Number returnNumber(){
System.out.println("super.returnNumber(..)");
return new Integer(1);
}

public Super returnObject(){
System.out.println("super.returnObject");
return new Super();
}

}

class Sub extends Super{

public Sub() {
super();
System.out.println("Sub instantiated..");
}


//this method is of no return type so we can not return any thing from this method.
public void noReturn(){
System.out.println("sub.noReturn");
}


/*
* This is also allowed,because primitive types can be returned as per Boxing rule provided my Java 5
*/
public Integer returnInteger(){
System.out.println("sub.returnInteger(..)");
return 1;
}

public int returnInt(int x){
System.out.println("sub.returnInt(..) : "+x);
return x;
}

/* This is not allowed.even 'int' and 'char' both are primitive type.
* public char returnInt(int x){
System.out.println("sub.returnInt(..)");
return x;
}*/

/*
* Return types during overriding does not matters only in this case,
* i.e when return type is subclass or superclass
*
*/
public Sub returnObject(){
System.out.println("sub.returnObject>> RETURN TYPE DOES NOT MATTERS IF INHERITENCE.");
return new Sub();
}

/*
*This is allowed because Integer is the Subclass of Number.
*
*/
public Integer returnNumber(){
System.out.println("sub.returnNumber(..)>> RETURN TYPE DOES NOT MATTERS IF INHERITENCE.");
return new Integer(1);

}

}

OUTPUT>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Super instantiated..
super.noReturn
super.returnInt(..) : 12
super.returnInteger(..)
super.returnNumber(..)
super.returnObject
Super instantiated..
Super instantiated..
Sub instantiated..
sub.noReturn
sub.returnInt(..) : 12
sub.returnInteger(..)
sub.returnNumber(..)>> RETURN TYPE DOES NOT MATTERS IF INHERITENCE.
sub.returnObject>> RETURN TYPE DOES NOT MATTERS IF INHERITENCE.
Super instantiated..
Sub instantiated..
***************************************************************************

Conclusion: "So we can say return type does not matters when it is in inheritance relation"