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 218733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:46:45+00:00 2026-05-11T18:46:45+00:00

In java, if a class implements Serializable but is abstract, should it have a

  • 0

In java, if a class implements Serializable but is abstract, should it have a serialVersionUID long declared, or do the subclasses only require that?

In this case it is indeed the intention that all the sub classes deal with serialization as the purpose of the type is to be used in RMI calls.

  • 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-05-11T18:46:45+00:00Added an answer on May 11, 2026 at 6:46 pm

    The serialVersionUID is provided to determine compatibility between a deseralized object and the current version of the class. As such, it isn’t really necessary in the first version of a class, or in this case, in an abstract base class. You’ll never have an instance of that abstract class to serialize/deserialize, so it doesn’t need a serialVersionUID.

    (Of course, it does generate a compiler warning, which you want to get rid of, right?)

    It turns out james’ comment is correct. The serialVersionUID of an abstract base class does get propagated to subclasses. In light of that, you do need the serialVersionUID in your base class.

    The code to test:

    import java.io.Serializable;
    
    public abstract class Base implements Serializable {
    
        private int x = 0;
        private int y = 0;
    
        private static final long serialVersionUID = 1L;
    
        public String toString()
        {
            return "Base X: " + x + ", Base Y: " + y;
        }
    }
    
    
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
    public class Sub extends Base {
    
        private int z = 0;
    
        private static final long serialVersionUID = 1000L;
    
        public String toString()
        {
            return super.toString() + ", Sub Z: " + z;
        }
    
        public static void main(String[] args)
        {
            Sub s1 = new Sub();
            System.out.println( s1.toString() );
    
            // Serialize the object and save it to a file
            try {
                FileOutputStream fout = new FileOutputStream("object.dat");
                ObjectOutputStream oos = new ObjectOutputStream(fout);
                oos.writeObject( s1 );
                oos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            Sub s2 = null;
            // Load the file and deserialize the object
            try {
                FileInputStream fin = new FileInputStream("object.dat");
                ObjectInputStream ois = new ObjectInputStream(fin);
                s2 = (Sub) ois.readObject();
                ois.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            System.out.println( s2.toString() );
        }
    }
    

    Run the main in Sub once to get it to create and save an object. Then change the serialVersionUID in the Base class, comment out the lines in main that save the object (so it doesn’t save it again, you just want to load the old one), and run it again. This will result in an exception

    java.io.InvalidClassException: Base; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 105k
  • Answers 105k
  • 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 Since comments cannot be nested in HTML, a regex can… May 11, 2026 at 8:46 pm
  • Editorial Team
    Editorial Team added an answer $_ -replace ([regex]::Matches($_ , "\d{4,}")), "DATESTAMP" Means in $_ replace… May 11, 2026 at 8:46 pm
  • Editorial Team
    Editorial Team added an answer Look at textField:shouldChangeCharactersInRange This method is called by the UITextFieldDelegate… May 11, 2026 at 8:46 pm

Related Questions

Is it possible to have objects stored in a data structure for the duration
I'm having a problem with my Seam code and I can't seem to figure
I want to implement drag and drop between two components within the same JVM.
I'm trying to access the message in a jthrowable while handing an exception generated
Are there any tools that auto-generate the hibernate POJOs by gathering information from the

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.