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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:28:16+00:00 2026-06-03T22:28:16+00:00

There’s a piece of code I’m currently looking like which serialises/deserialises data. The class

  • 0

There’s a piece of code I’m currently looking like which serialises/deserialises data.

The class structure looks something like this:

public class Field
{
    public abstract void writeField(DataOutputStream out);

    public abstract int getFieldID();

    ...

    protected static final int INT_FIELD = 1;
    protected static final int BOOL_FIELD = 2;

    ...

    public void write(DataOutputStream out)
    {
        out.writeInt(getFieldID());
        writeField(out);
    }

    ...
}

public class IntField extends Field
{
    public int v;

    public void writeField(DataOutputStream out)
    {
        out.writeInt(v);
    }

    public int getFieldID()
    {
        return Field.INT_FIELD;
    }
}

I feel like the way getFieldID is implemented is not the best.

Writing an abstract method to return a unique identifier for that class feels like a bad smell.

Is there better ways to go about implementing this?

  • 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-03T22:28:18+00:00Added an answer on June 3, 2026 at 10:28 pm

    Listing subclass-specific constants in a base class is not that great an idea, because ideally a superclass should not know about its subclasses. Just listing the constants in the base is not as bad as using them to differentiate among subclasses in the code of the base class.

    Ideally, you should move the constants into their respective subclasses. If you cannot do it without breaking a compile, you’ve indeed stumbled upon a prime candidate for refactoring.

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

Sidebar

Related Questions

There are few apps like Strava which records users movements using GPS. It also
There are several shading languages available today like GLSL, HLSL, CG, which one to
I would like to run a str_replace or preg_replace which looks for certain words
There is something I miss with the notion of Synchronizing code in Android. Scenario
There are a few ways to get class-like behavior in javascript, the most common
There's a combobox event in my code: self.combobox1.Bind(wx.EVT_COMBOBOX, self.onActionCombobox1) It executes the function def
There's a bug in FireBug: I accidentally clicked where the line numbers are, which
There is a div (form) which opens on the click of a button. There
There is a text file with 1,000,000 lines of code floating around and Ctrl+F
There is a class that's a fully fledged UIViewController and when that page loads

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.