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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:20:18+00:00 2026-06-10T22:20:18+00:00

After much searching I’ve found lots of relevant questions to normal classes, but none

  • 0

After much searching I’ve found lots of relevant questions to normal classes, but none that really relate to enums.

This works fine:

package list;

public interface Testing { 
    //treat as a tag interface.
}
package list;

public class Foo implements Testing {
}
package list;

import java.util.ArrayList;
import java.util.List;

public class Bar {
    public Bar(){
        List<Foo> myList = new ArrayList<Foo>();
        myList.add(new Foo());
        testList(myList);
        testMethod(new Foo());
    }

    public void testMethod(Testing myInstance){
        //do nothing
    }

    public <Testing> void testList(List<Testing> myList){
        //do nothing
    }
}

However, my problem comes in the understanding of what happens when I try and do the same with
Enums
instead of
Foo

The example here is there is an interface definition that is defined as accepting an interface parameter which, in reality, is implemented by an enum that implements the interface:

I have two interfaces:

public interface MyEnumInterface {
  //implemented by Enums
}
import java.util.List;

public interface SecondInterface {
    public MyEnumInterface testMe(String myString);
    public <MyEnumInterface> List<MyEnumInterface> testingList();
    public List <MyEnumInterface> enumTestOne(String anotherString);
    public List <MyEnumInterface> enumTestTwo(String anotherString);
}

Two emum as follows:

public enum MyEnum implements MyEnumInterface{
    VALUE_ONE,
    VALUE_TWO,
    VALUE_THREE;
}
public enum MyEnum2 implements MyEnumInterface{
    VALUE_A,
}

And a test java class:

import java.util.ArrayList;
import java.util.List;

class MyClass implements SecondInterface{

    public MyClass(){
        single(testMe(""));
        enumTestOne("");
        enumTestTwo("");
    }
    public MyEnum testMe(String someParam){
            return MyEnum.VALUE_ONE;
    }

    public List<MyEnumInterface> enumTestOne(String anotherString) {
        List<MyEnum> returnL = new ArrayList<MyEnum>();
        returnL.add(MyEnum.VALUE_ONE);
        returnL.add(MyEnum.VALUE_THREE);
        return returnL;
    }

    public List<MyEnumInterface> enumTestTwo(String anotherString) {
        List<MyEnum2> returnL = new ArrayList<MyEnum2>();
        returnL.add(MyEnum2.VALUE_A);
        return returnL;
    }


    public List<MyEnum> testingList(){
        List<MyEnum> returnL = new ArrayList<MyEnum>();
        returnL.add(MyEnum.VALUE_ONE);
        returnL.add(MyEnum.VALUE_THREE);
        return returnL;
    }


    public void single(MyEnumInterface val){
        System.out.println("Value is " + val);
    }


    public static void main(String[] args){
        MyClass clazz = new MyClass();
    }
}

I’m battling with the problem that the return type of enumTestOne(..) is coded as List but the interface specifies a List. It works fine when it’s an individual instance of the Enum ‘MyEnum’ but when it’s a list of values it seems to fail with a:

"error: method listing in class MyClass cannot be applied to given types;
required: List
found: List
reason: actual argument List cannot be converted to List by method invocation conversion"

I’m not sure of a way around this – I’ve tried adding the declaration you see above so that it reads
public <MyEnumInterface> List<MyEnumInterface> testingList();
but that then gives me "warnings" about unchecked casts whilst continuing to fail

This all stems from my lack of deeper understanding around Generics and how they relate to Enums.

I’ve seen some interesting declarations with respect to
<? extends Enum<E> & SecondInterface>
but that stretches my understanding. Please help!

As an aside, am I correct in thinking that the generic declarations before the return class are used to specify "T" for type erosion purposes?

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

    If you want to allow the implementing class to define which implementation of MyEnumInterface to use, you can specify the return type of the interface methods as a List of any MyEnumInterface.

    public List<? extends MyEnumInterface> enumTestOne(String anotherString);
    public List<? extends MyEnumInterface> enumTestTwo(String anotherString);
    

    The implementations of these methods can then define the concrete return type.

    public List<MyEnum> enumTestOne(String anotherString) {
        List<MyEnum> returnL = new ArrayList<MyEnum>();
        returnL.add(MyEnum.VALUE_ONE);
        returnL.add(MyEnum.VALUE_THREE);
        return returnL;
    }
    
    public List<MyEnum2> enumTestTwo(String anotherString) {
        List<MyEnum2> returnL = new ArrayList<MyEnum2>();
        returnL.add(MyEnum2.VALUE_A);
        return returnL;
    }
    

    By the way, you can do the same in “reverse” with super. This specifies that any type that is a supertype of the given type may be used.

    public void addToList(List<? super MyEnum> list) {
        list.add(MyEnum.VALUE_ONE);
    }
    
    addToList(new ArrayList<MyEnum>());
    addToList(new ArrayList<MyEnumInterface>());
    

    See also

    Java Generics (Wildcards)

    Why is SomeClass not equivalent to SomeClass in Java generic types?

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

Sidebar

Related Questions

After much searching, I have not found a satisfactory method that is easy to
After much reading it seems that, really, the only way to read a number
After much searching and looking extensively, I have a question that is undoubtedly dumb
After much searching and trials, I am stuck... I have two classes, one is
After much searching the answer seems to be no, but I thought I'd ask
After much searching, I found the download for the eclipse version of jalopy .
It appears after much searching that there seems to be a common problem when
This might be a very simple question but after much searching on different forums
After much searching and googling I am coming back to the well. I have
I know about $(Delphi) and $EDNAME but after much seaching I cant find a

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.