1 Reply Latest reply on Sep 19, 2007 11:09 AM by 807605

    Java serialization

    807605
      Assuming that the serializeBanana2() and the deserializeBanana2()
      methods will correctly use Java serialization and given:



      13. import java.io.*;

      14. class Food {Food() { System.out.print(�1�); } }


      15. class Fruit extends Food implements Serializable {
      16. Fruit() { System.out.print(�2�); } }


      17. public class Banana2 extends Fruit { int size = 42;
      18. public static void main(String [] args) {
      19. Banana2 b = new Banana2();
      20. b.serializeBanana2(b); // assume correct serialization
      21. b = b.deserializeBanana2(b); // assume correct
      22. System.out.println(� restored �+ b.size + � �); }
      23. // more Banana2 methods
      24. }


      What is the result?
      A. 12 restored 42
      B. 121 restored 42
      C. 1212 restored 42

      I think that the answer is A, but this is not correct. The concert answer is B. Does anybody know why is B correct?
        • 1. Re: Java serialization
          807605
          If you are a serializable class, but your superclass is NOT serializable, then any instance variables you INHERIT from that superclass will be reset to the values they were given during the original construction of the object. This is because the non-serializable class constructor WILL run!