• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Serialisation - K&B

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic