Thursday, June 4, 2009

Serialization Inheritence

Serialization in case of Inheritence:
class Animal implements Serializable
{
int i=10;
}
class Dog extends Animal
{
int j=20;

}

When ever we are adding a child class object to a file ,parent class object will not save to the file .Because parent class objects are not part of state of the child class object.

If any variable is inherited from the perent that variable will also add to the file as a part of child class object.

example:

class SerialDemo

{
public static void main(String arg[])
{
Dog d=new Dog();
System.out.println(d.i+”……..”+d.j);

FileOutputStream fos=new FileOutputStream(“abc.txt”);
ObjectOutputStream oos=new ObjectOutputStream(fos);
Oos.writeObject(d);
FileInputStream fis=new FileInputStream(“abc.txt’);
ObjectInputStream ois=new ObjectInputStream(fis);
Dog d1=9Dog)ois.readObject();
System.out.println(d.i+”……..”+d.j);
}
}
  • If the parent class is serializable by default every child class is also serializable.
  • If we are writing any servlet by extending GenericServlet or HttpServlet it is always Serializable because GenericServlet already implements serializable interface

Note: By just simply seeing a class we can’t tell whether it is serializable or not .We have to check any parent class of this class of this class is serializable or not .If none of the parent class is not serializable then then only we can decode the given class is not serializable.

Serialization in case of Inheritence example:

class Animal
{
int i=10;
}
class Dog extends Animal implements serializable
{
int j=20;
}
class SerialDemo
{
public static void main(String arg[])throws Exception
{
Dog d=new Dog();
d.i=1000;
d.j=2000;

FileOutputStream fos=new FileOutputStream(“abc.txt”);
ObjectOutputStream oos=new ObjectOutputStream(fos);
Oos.writeObject(d);
FileInputStream fis=new FileInputStream(“abc.txt’);
ObjectInputStream ois=new ObjectInputStream(fis);
Dog d1=9Dog)ois.readObject();
System.out.println(d.i+”……..”+d.j);
}
}
  • If the child class is serializable and the parent is non serializable still we are allowed to save the child class objects to the file.
  • If any variable is inherited from the non-serializable parent its value is not allowed to save to the file as its corresponding class is non- serializable.
  • Instead of original value ,default values will save to the file.
  • At the time of de serialization to perform initialization for instance variable which are inherited from non- serializable parents its objects will create .After creation of parent class object ,the values of inherited values will be assigned.
  • At the time of deserialization of an object if any variable is inherited from non- serializable parent then the JVM creates parent class object by calling default no argument constructor.
  • If the parent class doesn’t contain any no argument constructor we can get a RTE saying Java.io.invalidClassException Dog :novalid constructor.

1 comments:

Anonymous said...

Hello,

I have a doubt here...

Suppose I am running following code:

class Animal{
int i;
ABC a;

public Animal(){
i=10;
a= new ABC("Default");
}
}
class ABC{

String s;
public ABC(String str){
s=str;}
}

class Dog extends Animal implements Serializable{
int j=20;
}

public class SerialDemo {
public static void main(String arg[])
{
Dog d=new Dog();

System.out.println(d.i+"…….."+d.j);
try{
FileOutputStream fos=new FileOutputStream("abc.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis=new FileInputStream("abc.txt");

ObjectInputStream ois=new ObjectInputStream(fis);
Dog d1=(Dog)ois.readObject();
System.out.println(d.i+"…….."+d.j);
System.out.println(d.a.s);
}catch (Exception e) {
e.printStackTrace();
}
}
}

In Your article you have mentioned that :
"If any variable is inherited from the non-serializable parent its value is not allowed to save to the file as its corresponding class is non- serializable"

Using above the state of the ABC class in DOG whcih has been inherited from non serialized parent class (Animal), should not be serialized while saving Dog. But I am geeeting that ABC is serialized using DOG.

Please let me know If I am missing something.

My emial is : roshan_its@yahoo.com

Thank you for your article...

Post a Comment