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

Serialization in the inheritance tree, head first java 2nd edition

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a paragraph in head first java 2nd edition which says the following,

"If the object has a non-serializable class
somewhere up its inheritance tree, the
constructor for that non-serializable class
will run along with any constructors above
that (even if they're serializable). Once the
constructor chaining begins, you can't stop it,
which means all superclasses, beginning with
the first non-serializable one, will reinitialize
their state."

on page number 443

My doubt is, how can there be a non serializable class having a serializable superclass? . This question is with reference to the statement,
"the
constructor for that non-serializable class
will run along with any constructors above
that (even if they're serializable). "

If a class is serializable , then all its sub classes are also serializable right?(it is stated so at pg 440 in the book.)
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John, Welcome to the Ranch !!!

If a class is serializable , then all its sub classes are also serializable right?


Yes, that's right but i suppose they are referring the case when starting superclass is non-serialized and after that sub-class is serialized.
 
John Drulo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is if a class is non serializable, then how can its super class be serializable? Because they say that even the constructor of those classes will run. But such a situation will never arise right?
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If a class is serializable , then all its sub classes are also serializable right?


Yes, that's right


No, it's not. What a subclass does has no bearing on the superclass. It can't possibly make a difference to whether a superclass is serializable or not, because the superclass knows nothing of any subclasses (and doesn't consider them during serialization).

It's actually the other way round: a non-serializable class can't have serializable subclasses, but there's no problem with serializable classes having non-serializable subclasses.

If you write a few example classes and subclasses to test all 4 combinations, and then write code to serialize and deserialize them, the concept should become clearer.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:No, it's not. What a subclass does has no bearing on the superclass.


No, but what it inherits does.

John's question was "If a class is serializable, then all its sub classes are also serializable right?", and I can understand his confusion. If a superclass implements the Serializable interface, then how can a subclass suddenly "not be Serializable"? - which is what the items he quoted would suggest.

@John: It's been a long time since I did any study on serialization in detail because, to be honest, it's not that vital (there are usually better mechanisms around for "persisting" an object if you need to); but what I do remember is that it doesn't work precisely like a normal interface. There's a LOT going on in the background when you serialize an object, and you can also write private methods to "augment" behaviour that aren't actually defined anywhere except in the Serializable interface itself.

So I'm afraid my answer to your question is: I don't know.

You may find some useful information in the API docs, or you could try Googling for something like "how does Java serialization work?". I did, and that netted me this article, which states:

  "If a hierarchy of classes is to be serialized, each class in the hierarchy must implement the serializable interface..." (my highlighting)

That statement alone (if true) says to me that Serializable is NOT a normal Java interface; so you shouldn't expect it to behave like one.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Drulo wrote:If a class is serializable , then all its sub classes are also serializable right? (it is stated so at pg 440 in the book.)


And the tutorials would seem to back you up:

  "A Java object is serializable if its class or any of its superclasses implements either the java.io.Serializable interface or its subinterface, java.io.Externalizable."

So it looks like that article I linked is wrong, and either your book is too, or it's badly worded.

However, I'm still pretty sure that you shouldn't treat Serialization as "just another Java interface". There's a LOT to know about it - especially about getting it right.

HIH

Winston
 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested following condition:

1) Super class is not serialized but sub class is:
output : Super class variables showing default value instead of the value i set them and for sub class it showing same value i set.

2) Super class is serialized but sub class is not explicitly:
output : All variables showing same value i set.

3) Now i created 1 more subclass (1 <- 2 <- 3) , total have 3 class, 1 is not serialized, 2nd is and in 3rd i mentioned explicitly
output : variables used in 1st class showing default value instead of value i set and rest of variables defined in subclass 2 and its subclass 3 showing
the value i set.

 
John Drulo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tushar Goel wrote:I tested following condition:

1) Super class is not serialized but sub class is:
output : Super class variables showing default value instead of the value i set them and for sub class it showing same value i set.

2) Super class is serialized but sub class is not explicitly:
output : All variables showing same value i set.

3) Now i created 1 more subclass (1 <- 2 <- 3) , total have 3 class, 1 is not serialized, 2nd is and in 3rd i mentioned explicitly
output : variables used in 1st class showing default value instead of value i set and rest of variables defined in subclass 2 and its subclass 3 showing
the value i set.




So, I think we can draw the following conclusion from the experiment you carried out:

If a superclass implements Serializable, all its sub classes also implicitly implement Serializable and hence are Serializable, thereby adhering to the rules of inheritance in Object Oriented Design Paradigm. Also, the interface Serializable is no different from other interfaces, atleast with regards to inheritance in this context.

Am I right?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Drulo wrote:If a superclass implements Serializable, all its sub classes also implicitly implement Serializable and hence are Serializable, thereby adhering to the rules of inheritance in Object Oriented Design Paradigm. Also, the interface Serializable is no different from other interfaces, atleast with regards to inheritance in this context.
Am I right?


I'd say so, but that "at least" bit is important, as are Tushar's observations about values.

Like I say, I've never had cause to delve into it particularly deeply, so I've generally relied on Joshua Bloch's advice in his excellent book. In particular:
  • "Implement Serializable judiciously".
  • "Because [“implements Serializable”] is so easy to do, there is a common misconception that serialization requires little effort on the part of the programmer. The truth is far more complex".
    and
  • "implementing Serializable...increases the likelihood of bugs and security holes. [...] Whether you accept the default behavior or override it, deserialization is a “hidden constructor”.

  • You should also learn about the @serial and @serialData tags if you plan on using it more than just trivially.

    HIH

    Winston
    reply
      Bookmark Topic Watch Topic
    • New Topic