• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

API Contents - I/O

 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From enthuware:-

What will the following code print?

import java.io.*;

class Booby{
int i; public Booby() {i=10; System.out.println("Booby");}
}
class Dooby extends Booby implements Serializable{
int j; public Dooby() {j=20; System.out.println("Dooby");}
}
class Tooby extends Dooby{
int k; public Tooby(){k=30; System.out.println("Tooby");}
}
public class TestClass{
public static void main(String[] args) throws Exception{
Tooby t=new Toooby();
t.i=100;
ObjectOutputStream oos=new ObjectOutputStream( new FileOutputStream ("c:\\temp\\tooby.o"));
oos.writeObject(t); oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileOutputStream("c:\\temp\\tooby.o"));
Tooby t2 = (Tooby)ois.readObject();
System.out.println(t2.i+""+t2.j+""+t2.k);
}
}

Answer is:
Booby
Dooby
Tooby
Booby
10 20 30

Please explain the flow. There could be typo as I couldn't copy it from drag and drop question.

Thanks,

Naveen Katoch
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first three lines come from constructing a "Tooby" object. The superclass constructors are run in order from the root of the hierarchy.

Note that Booby is not Serializable. When nonserializable base class(es) of a serializable class is deserialized, the constructor(s) is (are) run; otherwise, deserializing an object does not cause its constructor to run.

Because the "Booby" constructor runs on the object, i is reset to its initial value, and the String "Booby" is printed.
 
xiao fei huang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you must notice that Class Bobby didn't implement Serializable, which means when Tooby object de-serialize happenning, constuctor of Class Boddy would be invoked.(So t.i = 10 but not 100. Because JVM won't get t.i from file c:\\temp\\tooby.o but from constructor of Boddy.)

Second,Dooby implemented Serializable interface and Tooby extended Dobby. Both Dooby and Tooby are serializable. So object t, which is a Tooby object, just simply read t.j and t.k values from file c:\temp\tooby.o but did not invoke their constructors. That's why t.j = 20, t.k = 30.

We could conclude the whole procedure of Serialzing and De-Serializing as follow:

SERIALIZING: (t.i do not write in file c:\temp\tooby.o because Booby is not serializable)->(t.j's written in file and j = 20)->(t.k's written in file and k = 30)

DESERIALIZING: (t.i do not exist in the file tooby.o, so JVM have to invoke constructor of Booby to make t.i hava a value. t.i = 10)->(t.j's read from file which value is 20)->(t.k's read from file which value is 30)

The test-maker try to confuse you by changing the value of i between serializing and de-serializing procedures. And hope you make mistake to cosider t.i=100 could be written in file tooby.o. But you have to clear that t.i is a member value of Booby class which is absolutely not a Serializable class. No matter what value t.i is, it's not gonna be written in file.

That's it.
[ November 28, 2008: Message edited by: xiao fei huang ]
 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest Friedman-Hill and xiao fei huang. Its now clear from your inputs.

Thanks again for your time,

Naveen Katoch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic