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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:21:11+00:00 2026-06-13T20:21:11+00:00

Suppose you have a generic interface: public interface MyInterface<T> { T doSomething(); } Is

  • 0

Suppose you have a generic interface:

public interface MyInterface<T> {
    T doSomething();
}

Is it possible to declare an enum that implements MyInterface<T>, but for which which every enum constant implements it for a different value of T? That is, given this enum:

public enum MyEnum {
    FOO,
    BAR,
    BAZ;
}

can we change it so that FOO implements MyInterface<Integer>, BAR implements MyInterface<String>, and BAZ implements MyInterface<List<MyOtherType>>, and make it so that MyEnum overall implements MyInterface<?>? It seems entirely feasible doing this raw, so it may be the case that it can be done in a typesafe manner.

  • 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-13T20:21:12+00:00Added an answer on June 13, 2026 at 8:21 pm

    No, as amalloy pointed out, Java doesn’t allow enums to be declared with type parameters. It becomes clear why if you think about the way enums are meant to be used, for example in a switch.

    Also consider how the language would implement generic enums – it isn’t trivial. For a generic enum MyEnum<T>, each enum constant would need to resolve T to some specific type, otherwise they wouldn’t be constants at all. Consider this:

    enum MyEnum<T> {
        FOO; // T is not resolved
    }
    

    What is T for FOO here? The language would need a new syntax just to be able to express it, for example:

    enum MyEnum<T> {
        FOO<String>;
    }
    

    So now we’re getting into added complexity for the language in order to support semantics that don’t have an overly compelling use case. It’s easy to see why the language designers would have simply nixed type parameters for enums.

    The workaround:

    You can emulate your desired pattern by simply not using an enum. Organize the implementations of your interface into a utility class:

    public class MyImplementations {
    
        public static final MyInterface<Integer> FOO =
                new MyInterface<Integer>() {
                    ...
                };
    
        public static final MyInterface<String> BAR =
                new MyInterface<String>() {
                    ...
                };
    
        public static final MyInterface<List<MyOtherType>> BAZ =
                new MyInterface<List<MyOtherType>>() {
                    ...
                };
    
        private MyImplementations() { }
    }
    

    The only thing inherently missing is a way to iterate over the different implementations, as you could have done with MyEnum.values() – but with a hypothetical MyEnum<T>, the most specific type you could iterate over would be MyEnum<?>.

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

Sidebar

Related Questions

Suppose you have a generic interface and an implementation: public interface MyInterface<T> { void
Suppose I have an interface for a service: public interface IFooService { void DoSomething();
Suppose we have an interface with a single generic method: public interface IExtender {
Suppose you have an interface like this: public interface IDoSomething<out T> { T DoSomething(object
Suppose I have a Generic abstract class that provides some default constructor functionality so
Suppose I have a pure virtual method in the base interface that returns to
Suppose I have these interfaces: public interface I1 { void foo(); } public interface
Suppose you have a COM interface ICOMInterface that is implemented by coclasses Coclass1 and
Suppose I have an interface called Interface, and a concrete class called Base, which,
Suppose you have the following Generic class heirarchy: public abstract class GenericBase<T> { T

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.