File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialisation - K&B

 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
Ken Truitt
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
M Srilatha
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob Ruth
Ranch Hand
Posts: 320
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Preethi Dev
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
will subclass constructor run after super class's constructor had been finished(while deserialization)?


Preparing Scjp5
 
Preethi Dev
Ranch Hand
Posts: 265
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob,
Could you explain 'copied in' values in detail?
I am not getting it.


Thanks
 
M Srilatha
Ranch Hand
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 137
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic