• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructors called when deserializing properly serialized subclass instance

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the ePractice exams purchased directly from Sun Microsystems for SCJP 5.0 310-055 exam.

Given:


If you attempt to deserialize a properly serialized instance of Ford, what is the result?

OPTIONS:
A - new Car
B - new Ford
C - new Car new Ford
D - new Ford new Car
E - Compilation fails
F - An exception is thrown at runtime

Sun's answer is as follows:
Option A - A superclass does NOT have to be Serializable but the constructor WILL run when a Serializable subclass instance is deserialized.

I disagree. I believe the answer is Option C. Even their explanation would support Option C.

Please, can someone confirm? Thank you,
Bonnie
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it has to be B i guess....the constructor of only superclass will run...letme check...yup i am true...check this code
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but if both Car and Ford were serializable then none of their constructors would have run when ford was deserialized
 
Bon Thanh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankur,

I ran the code that you supplied, and the output is:

animal1
DOg
animal

So it seems that 3 constructors were called in this order:
1. Animal(int x)
2. Dog(int x, String s)
3. Animal()

Now I'm even more confused!
Bonnie
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dude the last Animal was called when deserialization took place.....1st and 2nd were called when you created a dog object before serializing it
 
Bon Thanh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I see...
So the superclass' constructor with no arguments is called. Is this always the case when deserializing subclass instances?
And if there is no explicit superclass' constructor with no arguments, then the default one provided by the compiler will be called?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. when the superclass implements Serializable then none of their constructors will be called....

2. when the superclass does not implement it then a no arg constructor of superclass will be called
 
Bon Thanh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your quick replies, Ankur!
Bonnie
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
happy to help....
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankur,

When I run when I run the same code wit no args version commented out, JVM is throwing InvalidClassException. Does that mean only no args constructor will be called when deserializing?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey you should not get an error while des....if there is no no arg const...compiler will create one for you.....what did you do exaclty in the code...did you remove the no arg animal() constructor only?
 
vijayakrishna Mikkilineni
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I removed no args animal constructor only.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup am getting it too.....let me check
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok so there has to be a arg constructor if the superclass does not implement serializable....but if animal is serializable then there is no need...try it out

 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, this can be easily settled by reading the Java Object Serialization Specification

The specification clearly states that...


For serializable objects, the no-arg constructor for the first non-serializable supertype is run. For serializable classes, the fields are initialized to the default value appropriate for its type. Then the fields of each class are restored by calling class-specific readObject methods, or if these are not defined, by calling the defaultReadObject method. Note that field initializers and constructors are not executed for serializable classes during deserialization [...]



Therefore, according to this, in the example of the original question, the correct answer would be A.
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The no arg constructor does not need to be added if you do not add any other constructors. That is because the JVM will put one in as long as there aren't any constructors in the class. Since you have the constructor with the int argument still there the JVM will not create a no arg constructor for you.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a little bit off topic, but aside from this discussion I gotta say, I love this question - simple and complicated all at the same time
 
reply
    Bookmark Topic Watch Topic
  • New Topic