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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:37:17+00:00 2026-05-12T18:37:17+00:00

How can I have an abstract enum, or some kind of base enum? In

  • 0

How can I have an abstract enum, or some kind of base enum?

In my common code I’d like a notion of an enum placeholder, MyItems, without tying myself to a concrete enum. Then in each of my projects I would have a concrete implementation.

E.g.
Common Code

public interface MyItems {
    // Marker interface
}

Project A

public enum Items implements MyItems {
     RED_CAR, BLUE_CAR, GREEN_CAR;
}

Project B

public enum Items implements MyItems {
    BROWN_TREE, GREEN_TREE;
}

This seems to work, but in my common code I can’t write a loop over my interface enum, since it’s not an enum. In my common code I’d like to write

for (MyItems item : MyItems.values())
    doSomething(item);

but I can’t because my interface is just a marker interface, and it doesn’t have a .values().

Any suggestions greatly appreciated. I don’t know if I’m trying in completely the wrong way.

  • 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-12T18:37:18+00:00Added an answer on May 12, 2026 at 6:37 pm

    It doesn’t really make sense to do this – because the values() method is static. To call it, you need to know the type you want to call it on.

    The closest you could come would be to have:

    public interface MyItemsFactory<T extends MyItems>
    {
        Iterable<T> values();
    }
    

    and then implement that in some generic way, e.g.

    public class EnumFactory<T extends Enum<T> & MyItems>
        implements MyItemsFactory<T>
    {
        private final Class<T> clazz;
    
        public EnumFactory(Class<T> clazz)
        {
            this.clazz = clazz;
        }
    
        public Iterable<T> values()
        {
            return EnumSet.allOf(clazz);
        }
    }
    

    But the basic rule is that polymorphism and static methods don’t mix. In your call:

    for(MyItems item : MyItems.values())
        doSomething(item);
    

    which implementation of MyItems would you expect it to iterate over? You could have loads of types implementing MyItems.

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

Sidebar

Related Questions

So, I have an abstract class like: public abstract class AbstractParent <E extends Enum<E>>
Can an abstract class have a constructor? If so, how can it be used
I have a variable in code that can have file path or url as
I have some code that I want to refactor. I have lots of methods
I'm trying to figure out some C code so that I can port it
In C# I have an intrusive tree structure that looks like this: public abstract
I'm trying to write some Java code for a set of enum classes. Each
How can I have a generic abstract class, let's say Animal, and have it
I have a abstract class which represents an immutable value. Like strings, the immutable
Ok so I know that you can't have an abstract static method, although 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.