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

Problem with serialization/deserialization

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys! I have a problem with serialization...not really serialization, but implementation of Parcelable interface in Android, anyways here goes:



Another class is supposed to inherit behaviour of class A and widen it a bit:



I was expecting that while serializing class B, methods writeToParcel() and readFromParcel() (calles via keyword super) of class A would take care of serialization of fields fieldA and fieldB, and the same methods of class B would take care of serializing of fieldC, but it doesn't happen I don't want to duplicate the same code again. What am I doing wrong?

EDIT: edited post after Jeff's comment, I hope now it fits the rules.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please note that ItDoesntWorkIsUseless(←click). You need to TellTheDetails(←click) as to exactly what's going wrong.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of things do stand out though:

1. Not related to your problem, but as a matter of good design, your fields should be private, not public.

2. Not related to your problem, but as a matter of style, you should declare only one variable per line.

3. I don't see CREATOR fields in your classes. Parcelable's javadocs say: "Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface."
 
Aleksey Vladimirovich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:A couple of things do stand out though:

1. Not related to your problem, but as a matter of good design, your fields should be private, not public.

2. Not related to your problem, but as a matter of style, you should declare only one variable per line.

3. I don't see CREATOR fields in your classes. Parcelable's javadocs say: "Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface."



1, 2 Yeah, I know about it and it is so in real project, here I simplified code to type it faster. 3 Added CREATOR and necessary constructors.
The thing is, that classes which inherit my base class (class A) work perfectly unless they override implementation of Parcelable interface...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aleksey Vladimirovich wrote:

Jeff Verdegan wrote:A couple of things do stand out though:

1. Not related to your problem, but as a matter of good design, your fields should be private, not public.

2. Not related to your problem, but as a matter of style, you should declare only one variable per line.

3. I don't see CREATOR fields in your classes. Parcelable's javadocs say: "Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.Creator interface."



1, 2 Yeah, I know about it and it is so in real project, here I simplified code to type it faster.



The thing is, that makes it harder to read your code, and is a distraction from the issue at hand. It's to your own benefit to make it as easy as possible for the folks you're asking for help.

3 Added CREATOR and necessary constructors.
The thing is, that classes which inherit my base class (class A) work perfectly unless they override implementation of Parcelable interface...



Again, "doesn't work" doesn't tell us anything.
 
Aleksey Vladimirovich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:Again, "doesn't work" doesn't tell us anything.


Man, I don't how can I put this another way...Assume I have class C (extends A), which adds/overrides some methods, but doesn't add any new fields and doesn't override Parcelable implementation...So for this class serialization goes perfectly, but once a subclass overrides implementation of Parcelable interface (like class B) it's inherited fields don't serialize at all (I tried to achieve that by calling super.writeToParcel() and super.readFromParcel()). Any ideas?
 
Aleksey Vladimirovich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:This looks Android-specific, rather than Beginning Java, so I'll move it.


Okay, but I wouldn't say it's very androidish problem, besides android thread is dead here...I asked a question once - no replies at all, and I wasn't the only.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aleksey Vladimirovich wrote:

Jeff Verdegan wrote:Again, "doesn't work" doesn't tell us anything.


Man, I don't how can I put this another way.



You can tell us in exactly what manner it "doesn't work". Does it give an error? Produce bad data? Produce no data? Produce different random results each time? Produce the right data, but in the wrong order? Leave stuff out? Add stuff?

once a subclass overrides implementation of Parcelable interface (like class B) it's inherited fields don't serialize at all [MOD: empahsis mine] (I tried to achieve that by calling super.writeToParcel() and super.readFromParcel()).



That's the kind of details we're looking for!

Any ideas?



Sorry. I've never used Parcelable myself. Just as a random guess, and not something I'd recommend using unless you can find out why it works (if it actually does): Maybe make the parent class abstract, and have it NOT have that static CREATOR field? (But still have it implement the write/read methods, and override them in the child classes.)

Or else maybe only parcel it through references to the child classes, not the parent class? If there's any compile-time reference to the static CREATOR field, which class's field gets used will be determined by the type of the reference, not the class of the object. Not sure if/how that would affect it, but it seems like it could be a possibility.


Again, though, those are just wild stabs. Investigate before relying on them.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aleksey Vladimirovich wrote:

Jeff Verdegan wrote:This looks Android-specific, rather than Beginning Java, so I'll move it.


Okay, but I wouldn't say it's very androidish problem, besides android thread is dead here...I asked a question once - no replies at all, and I wasn't the only.



Unless there's something crucial you're not telling us, it seems related to how Parcelable is used, and that's an Adroid class.

If you don't get any more answers in the next day or so, you can create a thread in Java in General with a link to this one, explaining that you did so because you weren't getting any responses here.
 
Aleksey Vladimirovich
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:If you don't get any more answers in the next day or so, you can create a thread in Java in General with a link to this one, explaining that you did so because you weren't getting any responses here.


Okay, I believe I've found what the problem was...It's not related to implementation of Parcelable it's just my inattentiveness I guess I should have some rest Anyways, thanks for your time, man, I appreciate it
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic