Originally Posted by
helloworld922
edit:
hmm.. never mind. I did some actual testing and I guess that in order to be Serializable, a class and all of it's super classes must be Serializable (except for Object, which can be written out no problem).
However, declaring A to be Serializable as mentioned before does allow B to be Serializable (again, it's important to make sure that all members of B are Serializable).
You can of course write your own custom serialization/deserialization code, but I think the checking for Serializable is done before the over-written methods can be called.
yeah, I did a small practical of serialization and found that attribute belongs to A(in our case age) will not be stored(Serillized) as A does not implements Serilizable interface but fileds of B will be.
Here I am sending you the code...
public class SerilizationExample {
public static void main(String [] args)
{
Employee e = new Employee();
e.setAge(24);
//e.age = 100;
e.name = "Param.....";
e.address = "Mumbai, India....";
e.SSN = 11122333;
e.number = 101;
try
{
FileOutputStream fileOut =
new FileOutputStream("employee.ser");
ObjectOutputStream out =
new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
}catch(IOException i)
{
i.printStackTrace();
}
}
}
class Human
{
public int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
class Employee extends Human implements java.io.Serializable
{
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck()
{
System.out.println("Mailing a check to " + name
+ " " + address);
}
}
class DeserializeDemo
{
public static void main(String [] args)
{
Employee e = null;
try
{
FileInputStream fileIn =
new FileInputStream("employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Age: " + e.getAge());
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
e.mailCheck();
}
}