wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regarding serialization? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regarding serialization?" Watch "regarding serialization?" New topic
Author

regarding serialization?

swati cha
Ranch Hand

Joined: Jul 04, 2006
Posts: 40
import java.io.*;
class Food {Food(){System.out.println("1");}
}
class Fruit extends Food implements Serializable{
Fruit(){System.out.println("2");}
}
public class Banana extends Fruit{
int size=42;
public static void main(String a[]){
Banana b=new Banana();
b.serializeBanana(b);//assume correct serialization
b=b.deserialize(b);//assume correct
System.out.println("restore" + b.size + " ");
}
}

why the output is 121 restore 42?

can any one explain the above code,Thnaks in advance.........
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40


b.serializeBanana(b);//assume correct serialization
b=b.deserialize(b);//assume correct


Well, if serialization is assumed to be correct. And deserialization is assumed to be correct... then what are you asking about? The rest of the program just creates the object and prints it.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tony Smith
Ranch Hand

Joined: Jul 07, 2007
Posts: 229
I think the 121 is because the constructor calls. When you have Banana b=new Banana(); It calls its parent constructors that's why you have 12. Then when you b=b.deserialize(b); it calls its parent constructor that is not serializable which gives you the last 1.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8803
    
    5
Hi Swati -

It's absolutely okay for you to post mock questions on this forum, however we have a policy that you should mention the source of the question, this helps the author!

Thanks,

Bert

p.s. Full disclosure is that this is one of our questions , but seriously, we'd like for you to post the sources of ALL the mock questions you'd like to discuss.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
saran bala
Greenhorn

Joined: Oct 17, 2007
Posts: 8
HI,
The key point here to note here is that the constructors are not called while deserializing the object.While serializing the object,the constructors will be called.
So while serializing the b,superclass constructors are called..output is 1 2.then while deserilasing the object,since the food is not serialised,its constructor is called once again and the output is 1 and finally restore and 42.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: regarding serialization?
 
Similar Threads
i am getting identifier expected error in line 4... can any one help me out
Generic classes
Why Byte boxing doesn't work here