I just started with this topic. My understanding says that , if you want to save instance variables of a class , so that it can be used later... you may use "serializable" interface with no methods defined... But i have a doubt that , what happens in inheritance... Suppose my superclass is not-serialized wheras sub-class is serialized...can i save my superclass instance variables ?? Although, i know this... the costructor of superclass is being called and it will run...but what dat mean? is it mean that , i can't save instance variable of superclass....
In the below program... the created file will store numerical values(43,23) or just variables "height" and "weight"???
import java.io.*;
class A {
String sex;
public void gone(String sex){
this.sex = sex;
System.out.println("i m " + sex);
}
}
public class serialio1 extends A implements Serializable {
int height;
int weight;
public static void main(String[] args){
serialio1 myBox = new serialio1();
myBox.go(43,23);
try{
FileOutputStream fs = new FileOutputStream("foo.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(myBox);
os.close();
FileInputStream fileStream = new FileInputStream("foo.ser");
ObjectInputStream ms = new ObjectInputStream(fileStream);
Object one = ms.readObject();
Object two = ms.readObject();
int h = (int) one;
int w = (int) two;
ms.close();
System.out.println("saved values" + h + w);
} catch(Exception ex){
ex.printStackTrace();
}
}
public void go(int height , int weight) {
this.height = height;
this.weight = weight;
System.out.println(" height is " + height);
System.out.println(" weight is " + weight);
}
}