File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes API Contents - I/O Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "API Contents - I/O" Watch "API Contents - I/O" New topic

API Contents - I/O

nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

From enthuware:-

What will the following code print?


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();
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();

Answer is:
10 20 30

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


Naveen Katoch
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

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.

[Jess in Action][AskingGoodQuestions]
xiao fei huang

Joined: Nov 28, 2008
Posts: 7
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 ]

I consider myselft as computer scientist.
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Thanks Ernest Friedman-Hill and xiao fei huang. Its now clear from your inputs.

Thanks again for your time,

Naveen Katoch
I agree. Here's the link:
subject: API Contents - I/O
It's not a secret anymore!