• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Clarification on concepts for Serialization

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just want to clarify some concepts on Serialization. These are my own examples based on K&B
Scenario 1:

In this case when Dog is serialized and deserialized the code will compile fine but will get a runtime exception because A is not serializable.

Scenario 2:

In this case we get NullPointerException because the Animal object comes
back as null after the Dog is deserialized. When you deserialize the Dog the Dog constructor will not run but the Animal constructor will and so gets default values. But that is not the state of the Animal Dog used . Is that why Animal is null?

Scenario 3:

We are ok here.

Scenario 4: involves inheritance

Here if the Dog object has been given values as
Dog d = new Dog(4, “Pete”);
Here the Dog constructor does not run when Dog is deserialized but the animal constructor will. bSo the numberOfLegs will be reset to 0 but the name of the dog is still Pete. But what about the call to super()?
I know this is a long question but I appreciate your input very much,

Thanks,
Meera

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scenario 2:

When you deserialize the Dog the Dog constructor will not run but the Animal constructor will and so gets default values.


In this case there is NO relationship that is "IS-A"(extends). So there is no point of construtor calling.
the null value is because of the trasisent keyword

Scenario 4

but the name of the dog is still Pete.


Dog class is serializable...So pete(name) is restored.

But what about the call to super()?


super call will ONLY be executed if th DOG construtor is executed.

Correct me if i dont make sense.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scenario 1: Every Member object should implement Serializble .

Scenario 2: Every member object initialized by null automatically as part of object and class construction.

Scenario 4: If Dog constructor is not running in deserialization process then HOW that method call will run?

so super(“dogFood”); will never run. REMEMBER transient variables got reset at deserialization process. So constructor of Animal will be called but DOG implements Serializble so constructor of DOG will not be called.
 
Deepesh Deomurari
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scenario 1: Every Member object should implement Serializable .

Scenario 2: Every member object initialized by null automatically as part of object and class construction.

Scenario 4: If Dog constructor is not running in deserialization process then HOW that method call will run?

so super(“dogFood”); will never run. REMEMBER transient variables got reset at deserialization process. So constructor of Animal will be called but DOG implements Serializable so constructor of DOG will not be called.
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well here there is a call to super constructor from Dog's constructor


So when Dog is deserialized it's constructor will not run but the Animal class's constructor will. I guess in this case Animal's instance variables will become set to their default values even though you use defaultWriteObject() and defaultReadObject() for Dog. Anyone else have an opinion?(Ankit, Punit, Ruben etc..?)

Thanks,
Meera
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Try this one.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to resurrect this zombie, but I think it might be worth doing so in this case. I modified Punit's code a little bit:


Output is:
In Animal
Dog name: tommy
Dog numberOfLegs: 10
Dog eat: null

It seems that when you implement readObject(), when deserialization takes place, the first thing that runs is the non-serializable superclass' initialization code. The calls to readInt and readObject are just overwriting the values set by that initialization code.
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it means that the Animal part of Dog is reset but the Dog's variable -- name is not. Thank you both Punit and Ruben for clarifying this concept.
Regards,
Meera
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic