tag:blogger.com,1999:blog-4796886846449158195.post7514546014703109642..comments2023-09-20T03:48:44.199-07:00Comments on Java Examples: Serialization InheritenceLaxman Ghttp://www.blogger.com/profile/11755286384905885095noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-4796886846449158195.post-67531487822070120912009-11-11T07:52:25.312-08:002009-11-11T07:52:25.312-08:00Hello,
I have a doubt here...
Suppose I am runni...Hello,<br /><br />I have a doubt here...<br /><br />Suppose I am running following code:<br /><br />class Animal{<br />int i;<br />ABC a;<br /><br />public Animal(){<br /> i=10;<br /> a= new ABC("Default");<br />}<br />}<br />class ABC{<br /> <br /> String s;<br /> public ABC(String str){<br /> s=str;}<br />}<br /><br />class Dog extends Animal implements Serializable{<br />int j=20;<br />}<br /><br />public class SerialDemo {<br /> public static void main(String arg[])<br /> {<br /> Dog d=new Dog();<br /> <br /> System.out.println(d.i+"…….."+d.j);<br /> try{<br /> FileOutputStream fos=new FileOutputStream("abc.txt");<br /> ObjectOutputStream oos=new ObjectOutputStream(fos);<br /> oos.writeObject(d);<br /> FileInputStream fis=new FileInputStream("abc.txt");<br /> <br /> ObjectInputStream ois=new ObjectInputStream(fis);<br /> Dog d1=(Dog)ois.readObject();<br /> System.out.println(d.i+"…….."+d.j);<br /> System.out.println(d.a.s);<br /> }catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br />}<br /><br />In Your article you have mentioned that :<br />"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"<br /><br />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.<br /><br />Please let me know If I am missing something.<br /><br />My emial is : roshan_its@yahoo.com<br /><br />Thank you for your article...Anonymousnoreply@blogger.com