Given the following code:.
public class Shape { protected transient String name; Shape() { this.name = "NoName"; } Shape(String name) { this.name = name; } }
public class Rectangle extends Shape { protected long num; Rectangle() { } Rectangle(String name, long num) { super(name); this.num = num; } }
import java.io.Serializable; public class Square extends Rectangle implements Serializable { private int side; Square(String name, long num, int side) { super(name, num); this.side = side; }/*from ww w . j a va 2 s.c o m*/ public String toString() { return "(" + name + ", " + num + ", " + side + ")"; } }
import java.io.*; public class Main { public static void main(String args[]) throws IOException, ClassNotFoundException { FileOutputStream outputFile = new FileOutputStream("storage.dat"); ObjectOutputStream outputStream = new ObjectOutputStream(outputFile); Square stud1 = new Square("Java", 100, 1); System.out.print(stud1);/* ww w.j av a2 s .c o m*/ outputStream.writeObject(stud1); outputStream.flush(); outputStream.close(); FileInputStream inputFile = new FileInputStream("storage.dat"); ObjectInputStream inputStream = new ObjectInputStream(inputFile); Square stud2 = (Square) inputStream.readObject(); System.out.println(stud2); inputStream.close(); } }
Which statement is true about the program?.
Select the one correct answer.
(a) It fails to compile.
(b) It compiles, but throws an exception at runtime.
(c) It prints (Java, 100, 1)(NoName, 0, 1).
(d) It prints (Java, 100, 1)(Java, 100, 1).
(e) It prints (Java, 100, 1)(null, 0, 1).
(c)
Note that only Square is Serializable.
The field name in the Shape class is transient.
During serialization of a Square object, the fields side and studNum
are included as part of the serialization process, but not the field name.
During deserialization, the default constructors of the superclass up the inheritance hierarchy of the Square class are called, as none of the superclass are Serializable.