Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:13:38+00:00 2026-06-17T21:13:38+00:00

Say I have an ArrayList<B> array of objects from a certain class B ,

  • 0

Say I have an ArrayList<B> array of objects from a certain class B, which extends A. B has an instance field bb and A a field aa. I know that saving array to a .dat-file using ObjectOutputStream requires that B (not just ArrayList!) implement Serializable. I’ve found, however, that when loading the object back from the file (using an ObjectInputStream):

 arrayLoaded = (ArrayList<B>)myObjIn.readObject();

the loaded array isn’t identical to the original array: In the particular case, arrayLoaded.get(0).bb has the same value as in array, but arrayLoaded.get(0).aa is “zeroed”. It has a default initialization value, regardless of its value when array was saved to file. However, this problem is solved by letting also A implement Serializable.

What bothers me is that this error is so subtle: no exception, no warning (in eclipse), nothing. Is there a reason for this or is this simply an oversight by the java developers? Do I just have to accept it and think hard about which classes in the hierarchy implement Serializable every time I want to use object IO streams?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:13:39+00:00Added an answer on June 17, 2026 at 9:13 pm

    Just because B implements Serializable, that does not retroactively include the fields of the non-serializable superclass in what gets serialized. (This makes sense, especially when you consider that being able to serialize private and package-private fields of any class just by extending it and implementing Serializable would violate its encapsulation.)

    A field declared in A will behave the same as a field declared as transient in B. There is a workaround however. From the documentation for Serializable:

    To allow subtypes of non-serializable classes to be serialized, the
    subtype may assume responsibility for saving and restoring the state
    of the supertype’s public, protected, and (if accessible) package
    fields. The subtype may assume this responsibility only if the class
    it extends has an accessible no-arg constructor to initialize the
    class’s state.

    So you will need to implement writeObject and readObject in B to handle the serialization/deserialization of A.aa.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java, say you have a class that wraps an ArrayList (or any collection)
Let's say I have an array list of objects, and each object has the
So let say I have an array of size 10 with index range from
I have an Array say: VMHArray = (12,22,34,4) Now have another Arraylist object say
Let's say I have a ArrayList<String> data; I'm adding some data into this Array,
I want to put some numbers from Excel to array. Let's say I have
I have an object that contains a list of other objects. Say it's as
I have a string which has say 3000 characters, now I want to split
Say I have a 3x4x5x6 java double array a that I unroll into an
I have an arraylist in Java that I fill with certain data, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.