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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:08:28+00:00 2026-05-31T19:08:28+00:00

I’m writing an abstract class in Java where I’d like a parameter of one

  • 0

I’m writing an abstract class in Java where I’d like a parameter of one of my abstract methods to be an enum, so I’ve got the following. Note that only subclasses will know what possible states it can have. (Please excuse the contrived example)

public abstract class StateTracker {
    public abstract boolean isInState(???????? state);
}

What is ????????? Therein lies the problem:

  • Ideally it would be an abstract enum called StateEnum that has no instances, and the clients of StateTracker could make their own concrete StateEnums. But we can’t do anything like that in Java.
  • I could make it an interface, but then I’d have no way of ensuring that the subclasses implement it as an enum, which is the goal.
  • I could change it to Enum < ? >. So I try that, and in my subclass I do something like this:

    public class MyStateTracker extends StateTracker {
    
        public enum MyState {
             BADSTATE_1, BADSTATE_2
        }
    
        @Override
        public boolean isInState(?!?!?!?! state) {
             // TODO Auto-generated method stub
        return false;
     }
    
    }
    

Now what is ?!?!?!?! Here are the possibilities:

  • MyState. This doesn’t compile since it doesn’t think that I’m overriding the abstract method.
  • Enum. Same problem.

How can I ensure that concrete classes use an emum?

  • 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-31T19:08:29+00:00Added an answer on May 31, 2026 at 7:08 pm

    It depends on whether you want to be able to ask a StateTracker whether it is in states declared by different enum types.

    If you want to lock down the states so that a given state tracker can only be queried for one type of enum, create a generic type:

    interface StateTracker<S extends Enum<S>> {
      boolean isInState(S state);
    }
    
    final class MyStateTracker implements StateTracker<MyStateTracker.MyState> {
      enum MyState { S1, S2 };
      private MyState state;
      ...
      @Override
      public boolean isInState(MyState state) {
        return state == this.state;
      }
    }
    

    If you have a couple of enum types defining states like this:

    enum MyState { S1, S2 }
    enum YourState { S1, S2 };
    

    … and you want to be able to ask a StateTracker whether it is in any state (defined by any enum, really, unless you also declare a State marker interface for your enum to implement), then create a generic method:

    interface StateTracker {
      <S extends Enum<S>> boolean isInState(S state);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

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.