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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:05:35+00:00 2026-06-07T03:05:35+00:00

I have a few classes which are the same level in a subtype hierarchy.

  • 0

I have a few classes which are the same “level” in a subtype hierarchy. I need to create an ID for each instance and I usually do this by having a parent class, containing a static long and I just increment it for every instance of the subclass.

75% of the code for the subtypes is the same- this made me prefer to use an abstract class (over an interface) as:

1) I can declare a static variable for the ID creator

2) I can put the code in the parent class and share it amongst the subtypes

Is this correct? I seem to rarely use interfaces. Should I only use interfaces when subtypes need the same methods, but different implementations and also when I do not need to initialize an attribute to be shared across the subtypes (like the ID creator)?

I had always been given the impression from colleagues that Interfaces were preferred to inheritance.

  • 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-07T03:05:37+00:00Added an answer on June 7, 2026 at 3:05 am

    If you have more than one group of classes that need ID generation, then I wouldn’t advise using inheritance because it constrains reuse. In that case why not build a separate abstraction that provides the service of unique ID generation? You’d give it a key and it would give you an ID that is unique across all invocations using the same key.

    public class IdGenerator 
    {
      private IdGenerator() {} // no instantiation or subclassing
      private static final ConcurrentMap<String, AtomicLong> ids =
        new ConcurrentHashMap<String, AtomicLong>();
    
      public static long id(String key) {
        AtomicLong al = ids.get(key);
        if (al == null) {
          final AtomicLong newL = new AtomicLong(0);
          al = ids.putIfAbsent(key, newL);
          if (al == null) al = newL;
        }
        return al.getAndIncrement();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many different small classes which have a few fields each, e.g. this:
I have a few classes which have a property which is unique to that
I have a few different classes which origin is a another class. I have
I have a few entities which are managed by custom NSManagedObject classes. They are
I have a few classes and I am having problems accessing properties defined in
I have a few custom UIViewControllers in my app which need to know the
I have a few classes that are instantiated using ninject Session Scoping - because
I have a few classes: Article ------- Content ID Magazine -------- Name Code And
I have a few classes in my current project where validation of Email/Website addresses
I have a few classes in a project that I inherited that are really

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.