Seethe following code:
import java.io.*;
class Animal
{ int ani;
Animal( int a)
{ System.out.println("Super Class Constructor: a: " +a);
}
}
class cat extends Animal implements Serializable
{ int num;
cat(int a,int b) {
super(100);
num = a ;
ani = b;
}
}
class Hello
{
public static void main (
String args[] )
{
cat c = new cat(2,5);
System.out.println("Before seria : num : " + c.num );
System.out.println("Before seria : ani : " + c.ani );
try {
FileOutputStream fs = new FileOutputStream("test.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(c);
os.close();
} catch(Exception e) { }
try {
FileInputStream fis = new FileInputStream("test.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
c = (cat) ois.readObject();
ois.close();
} catch(Exception e) { }
System.out.println("After seria: num: "+ c.num );
System.out.println("After seria: num: "+ c.ani );
}
}
OutPut is :
Super Class Constructor : a :100
Before seria : num : 2
Before seria : ani : 5
Before seria : num : 2
Before seria : ani : 5
My Doubt is :
Class Animal is nor serializable , so it should print ani value as ZERO after deserialization , and here super class constructor should run. Bur it is not happening.
WHY IT IS LIKE THIS ??
It is working fine when super class has DEFAULT constructor .
Regards,
Sharath