File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialisation -  K&B 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 "Serialisation -  K&B" Watch "Serialisation -  K&B" New topic
Author

Serialisation - K&B

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
This is from K&B.
It's mentioned in K&B -
"The key here is that because Animal is not serializable, when the Dog is was deserialized, the Animal constructor run and reset the the Dog's inhertited weight variable


But I thought the polymorphism did not apply to instance variables and that Dog couldnot inherit the Animal variable - just access it.





The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
Animal doesn't implement Serializable, so the behavior
in that case is that you can go ahead and serialize an instance of Dog,
but when you deserialize, Animal's constructor will run--and this has the
effect of RESETTING your instance of Dog's inherited weight field to
its default value from Animal--42.

As far as inheritance, sure weight is defined in Animal first, so it's
implicitly in Dog.
[ September 28, 2008: Message edited by: Ken Truitt ]

SCJP 88% | SCWCD 84%
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
I thought the polymorphism did not apply to instance variables and that Dog couldnot inherit the Animal variable - just access it.


I am not sure why you are referring to polymorphism here.
But as far as inheritance is concerned, all the members(instance variable/methods) which are visible to the subclasses are inherited by the subclasses. You can access those inherited members as if there are declared in the subclasses itself.


Thanks,<br />Srilatha M
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Just one more point of view on this:

1) Inheritance issue: if a superclass defines a variable and it is not defined with private access, then a subclass can reference it and modify it as if it had defined it itself.

2) Serialization issue: If a subclass is Serializable but the superclass is not: Then, when deserialized, the subclass constructor will NOT be run so that it's serialized variable maintain their "copied in" values. However, since the superclass was NOT serializable, it's constructor WILL be run and any variables that are defined and initialized in the superclass will be initialized and lose their "copied in" value.


------------------------
Bob
SCJP - 86% - June 11, 2009
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi,
will subclass constructor run after super class's constructor had been finished(while deserialization)?


Preparing Scjp5
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Hi Bob,
Could you explain 'copied in' values in detail?
I am not getting it.


Thanks
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Originally posted by Bob Ruth:
Just one more point of view on this:

1) Inheritance issue: if a superclass defines a variable and it is not defined with private access, then a subclass can reference it and modify it as if it had defined it itself.

2) Serialization issue: If a subclass is Serializable but the superclass is not: Then, when deserialized, the subclass constructor will NOT be run so that it's serialized variable maintain their "copied in" values. However, since the superclass was NOT serializable, it's constructor WILL be run and any variables that are defined and initialized in the superclass will be initialized and lose their "copied in" value.


One addition for 1 point: Super class's instance variables with the default access are not inherited by Sub classes which are in different package.

@Arun:

"Copied in" means the values which are copied during the serialization process.

[ October 01, 2008: Message edited by: M SRILATHA ]
[ October 01, 2008: Message edited by: M SRILATHA ]
Tanvi Narula
Greenhorn

Joined: Sep 30, 2008
Posts: 9
[One addition for 1 point: Super class's instance variables with the default access are not accessible for Sub classes.]

Why not ??
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Originally posted by Tanvi Narula:
[One addition for 1 point: Super class's instance variables with the default access are not accessible for Sub classes.]

Why not ??


This is true if the subclasses are in a different package. I have edited my previous post clearly!

I hope this is clear now!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialisation - K&B