• 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

Q on object serialization

 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A
{
String value;

public A()
{
value = �A Value�;
}
}

class B extends A implements java.io.Serializable
{
public B()
{
value = �B Value�;
}
}

What issues I will have when serializing class B?
How should I fix it?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No issues that I can think of, so nothing to fix. Are you having a problem, or imagining one?
 
Jane Somerfield
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm imagining one.

The "value" variable is defined in the super class A. Would that be an issue?
I feel comfortable with following:

class A implements java.io.Serializable
{
String value;

public A()
{
value = �A Value�;
}
}

class B extends A
{
public B()
{
value = �B Value�;
}
}

But I do not have the control on the super class A.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, I was a little hasty. There is a sort of a problem, in that the "A" parts of a B object won't be serialized, and therefore a deserialized "B" object will have "value" equal to null unless you do something special. You can deal with this by writing a writeObject() and readObject() method, something like



If you don't have access to the superclass members, and if there's no init() sort of method you can use to initialize the superclass, then there's more trouble.
 
Jane Somerfield
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will ask the people who own Class A to implement java.io.Serializable. It will simplify my life...
 
reply
    Bookmark Topic Watch Topic
  • New Topic