Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization for a class not implemention serializable  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read folloing statement from Head First Java "If any class of a super class is serializable, the subclass is automatically serializable even if the subclass doesn't explicitly declare implements Serializable"

class A implements Serializable{}

class B extends A{}

class C extends B{}

class D extends B implements Serializable{}

From the statement above, B is serializable. But does C become automatically serializable ?

If an object of class D is serialized and then deserialized, will this reinitialize constructor of B OR will it bring back the status of B ?
If an object of class C is serialized , will it be serializable ? Will implicit serializable status of class B also applies to class C ORR
does it apply to classes in immediate inheritance ? If class C is serializable, and then its deserialized, will this reinitialize constructor of B OR will it bring back the status of B (As B is implicitly serializable) ?

Thanks
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:I read folloing statement from Head First Java "If any class of a super class is serializable, the subclass is automatically serializable even if the subclass doesn't explicitly declare implements Serializable"

class A implements Serializable{}
class B extends A{}
class C extends B{}
class D extends B implements Serializable{}
From the statement above, B is serializable. But does C become automatically serializable ?


Yes. C is serializable because any of it's super class (A in this case) is serializable.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are saying that if any super class is serializable, then all sub classes, hoever deep in hierarchy will all be automatically serializable.

There is another statement in Head First Java "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 constructor above that(even if they are 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"

From this I understand as follows

class A (serializable) -> class B (explicitly non-serializable , but becomes serializable because of inheritance from A) -> class C(explicitly non-serializable , but becomes serializable because of inheritance from A) -> class D (explicitly serializable and also serializable because of inheritance from A)

So, when object of class D is serialized and then deserialized, all the variables should get their status back as all classes in inheritance are serializable either explicitly or by inheritance. So in this situation, whats the importance of above statement, in hich it states "......non-serializable class will run along with any constructor above that(even if they are serializable)"

From this I understand that lowest class in inheritance tree is serializable, then its super class is non-serializable (Thats when constructor for that non-serializable is initialized). Super class to this class is serializable, but still its constructor will be reinitialized. Logically speaking, why would some class in between two serializable classes be non-serializable ?

Which part is not right in here ? Please answer all question of previous post also.

Thanks
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the statement above, B is serializable. But does C become automatically serializable ?
Yep. Most super class(A) is Serialized.
So class B is automatically Serialized because it has qualities of A.
So C is Serialized because it has qualities of B(B and A).

If an object of class D is serialized and then deserialized, will this reinitialize constructor of B OR will it bring back the status of B ?
I don't understand what did you meant in "class D is serialized and then deserialized".

If an object of class C is serialized , will it be serializable ?
Yep.

Will implicit serializable status of class B also applies to class C OR does it apply to classes in immediate inheritance ?
If super class is Serializable all the sub classes are Serialized. not only immediate sub class.

If class C is serializable, and then its deserialized, will this reinitialize constructor of B OR will it bring back the status of B (As B is implicitly serializable) ?
Again, I don't understand what did you meant in "class C is serializable, and then its deserialized".
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By deserialization , I mean bringing a serialized object back to its state using obj.readObject() of ObjectOutputStream

----------------------------------------------------------------------------------------------------------------------------------------------------

Will implicit serializable status of class B also applies to class C OR does it apply to classes in immediate inheritance ?
If super class is Serializable all the sub classes are Serialized. not only immediate sub class.
Ok, so all classes in an inheritance tree automatically become serializable when super class is serializable. But there is another statement in Head First Java book (markes in bold in the quote below) which confuses me
"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 constructor above that(even if they are 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"


From this statement I understand that top class and a sub class somewhere down the inheritance are explicitly serializable and class in between these two classes is not explicitly serializable.
What exactly does this statement in bold letters say ?
If a top class in inheritance tree is serializable, then why would a class in hierarchy would be not serializable ? Whats not right in here ?

Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:
"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 constructor above that(even if they are 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"


From this statement I understand that top class and a sub class somewhere down the inheritance are explicitly serializable and class in between these two classes is not explicitly serializable.
What exactly does this statement in bold letters say ?
If a top class in inheritance tree is serializable, then why would a class in hierarchy would be not serializable ? Whats not right in here ?


I don't think that there is such a thing as "implicitly serializable" or "explicitly serializable" -- either something is serializable or it is not. And that is determined on whether it passes the IS-A test. In your example, an instance of D IS-A instance of B, which IS-A instance of A, which IS-A instance of Serializable. This means that an instance of class D IS-A Serializable already, and the "implements Serializable" declaration of class D is redundant -- ie. it is not necessary.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!