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

  • Home
  • SEARCH
  • 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 42719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:22:56+00:00 2026-05-10T15:22:56+00:00

I have: class MyClass extends MyClass2 implements Serializable { //… } In MyClass2 is

  • 0

I have:

class MyClass extends MyClass2 implements Serializable {   //... } 

In MyClass2 is a property that is not serializable. How can I serialize (and de-serialize) this object?

Correction: MyClass2 is, of course, not an interface but a class.

  • 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. 2026-05-10T15:22:56+00:00Added an answer on May 10, 2026 at 3:22 pm

    As someone else noted, chapter 11 of Josh Bloch’s Effective Java is an indispensible resource on Java Serialization.

    A couple points from that chapter pertinent to your question:

    • assuming you want to serialize the state of the non-serializable field in MyClass2, that field must be accessible to MyClass, either directly or through getters and setters. MyClass will have to implement custom serialization by providing readObject and writeObject methods.
    • the non-serializable field’s Class must have an API to allow getting it’s state (for writing to the object stream) and then instantiating a new instance with that state (when later reading from the object stream.)
    • per Item 74 of Effective Java, MyClass2 must have a no-arg constructor accessible to MyClass, otherwise it is impossible for MyClass to extend MyClass2 and implement Serializable.

    I’ve written a quick example below illustrating this.

     class MyClass extends MyClass2 implements Serializable{    public MyClass(int quantity) {     setNonSerializableProperty(new NonSerializableClass(quantity));   }    private void writeObject(java.io.ObjectOutputStream out)   throws IOException{     // note, here we don't need out.defaultWriteObject(); because     // MyClass has no other state to serialize     out.writeInt(super.getNonSerializableProperty().getQuantity());   }    private void readObject(java.io.ObjectInputStream in)   throws IOException {     // note, here we don't need in.defaultReadObject();     // because MyClass has no other state to deserialize     super.setNonSerializableProperty(new NonSerializableClass(in.readInt()));   } }  /* this class must have no-arg constructor accessible to MyClass */ class MyClass2 {    /* this property must be gettable/settable by MyClass.  It cannot be final, therefore. */   private NonSerializableClass nonSerializableProperty;    public void setNonSerializableProperty(NonSerializableClass nonSerializableProperty) {     this.nonSerializableProperty = nonSerializableProperty;   }    public NonSerializableClass getNonSerializableProperty() {     return nonSerializableProperty;   } }  class NonSerializableClass{    private final int quantity;    public NonSerializableClass(int quantity){     this.quantity = quantity;   }    public int getQuantity() {     return quantity;   } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 98k
  • Answers 98k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The simplest way by far is using a Setup project.… May 11, 2026 at 7:39 pm
  • Editorial Team
    Editorial Team added an answer UITextField and UITextView both have a text property that you… May 11, 2026 at 7:39 pm
  • Editorial Team
    Editorial Team added an answer If you want to get types of variables offline with… May 11, 2026 at 7:39 pm

Related Questions

I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
Is it possible to narrow the type of a field in a Java class
Is there a way to enforce/limit the types that are passed to primitives? (bool,
So, in Java, the first line of your constructor HAS to be a call
I would like to do simple (looping) animation (moving, changing alpha etc) in Haxe

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.