when we deserialize the dog object the Animal class constructor is run and the weight variable gets the value of 42 instead of the value it had when the dog object was serialized.
Swastik
Swastik
Swastik Dey wrote:Puja,
Yes you are right. As per my understanding the state of the object is saved, but the variable weight belongs to the non-serialized class, and thats why not persisted. If you declare another instance variable with the same name in Dog class, you should get the saved state. Correct me if I am wrong.
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Tommy Delson wrote:
Swastik Dey wrote:Puja,
Yes you are right. As per my understanding the state of the object is saved, but the variable weight belongs to the non-serialized class, and thats why not persisted. If you declare another instance variable with the same name in Dog class, you should get the saved state. Correct me if I am wrong.
That's INCORRECT "If you declare another instance variable with the same name in Dog class, you should get the saved state. Correct me if I am wrong."
Andreas Svenkson wrote:
Tommy Delson wrote:
Swastik Dey wrote:Puja,
Yes you are right. As per my understanding the state of the object is saved, but the variable weight belongs to the non-serialized class, and thats why not persisted. If you declare another instance variable with the same name in Dog class, you should get the saved state. Correct me if I am wrong.
That's INCORRECT "If you declare another instance variable with the same name in Dog class, you should get the saved state. Correct me if I am wrong."
No, that's perfectly correct. By declaring a new instance variable with the same name, one is shadowing/hiding the inherited variable. The "new" member variable is indeed part of the new object, which is serializable, and therefor that variable will be saved and restored. But the inherited variable is still there, accessible via inheritance, and not restored - which I believe is exactly what Swastik was saying.
// Andreas
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Andreas Svenkson wrote:I think you are actually agreeing with what I and Swastik said, atleast if I get you right
![]()
If so we can agree on the following:
1: The inherited weight var from Animal will not be restored when deserializing a Dog object. Instead, the dog will have a weight variable with the value of 42, since it has been reinitialized in the Animal class - just as Swastik said.
// Andreas
2: We can shadow/hide the inherited variable (for whatever reason), resulting in a new variable in the Dog class with the same name. This one would be both serialized/deserialized upon doing so on a Dog object.
Now, whether or not one should/would want to hide an inherited variable like this, is a different discussion. Swastik only brought it up because that is the only way in which deserializing a Dog object would also deserialize the (new) weight variable.
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
OCPJP 6.0