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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:10:33+00:00 2026-05-19T03:10:33+00:00

I spent quite a while with Google to find some information on this topic,

  • 0

I spent quite a while with Google to find some information on this topic, but results relating to both Java enums and covariant return types were pretty much non-existent.

So: is it possible to use covariant return types with enum methods where you define a method in the enum class and then override it in the instances, like so:

package enumcovariance.test;

public enum CovariantEnum {

    INT_INSTANCE(new Integer(3)) {
        @Override
        public Integer getData () {
            return (Integer) super.getData();
        }
    },

    STR_INSTANCE("Hello world") {
        @Override
        public String getData () {
            return (String) super.getData();
        }
    };

    private final Object data;

    private CovariantEnum(Object data) {
        this.data = data;
    }

    public Object getData () {
        return data;
    }

}

And then to take advantage of the covariance like so:

package enumcovariance.test;

import org.junit.Test;


public class CovariantEnumTest {

    @Test
    public void intEnumTest () {
        Integer i = CovariantEnum.INT_INSTANCE.getData();
    }

    @Test
    public void strEnumTest() {
        String s = CovariantEnum.STR_INSTANCE.getData();
    }

}

In this case the compiler is fine with my enum definition, but the test case fails to compile, saying Object cannot be converted to Integer (or String). Apparently the compiler only looks at the base definition of the method, not the overriding method.
With a different enum definition I had the base method abstract, but that still didn’t work.

I’m thinking it’s something complex to do with the way enums are transformed during the compile process that prevents it from working, but I want to be sure it’s not just me doing something silly.

Note that this test case is admittedly very contrieved, in my actual enum this functionality would be more useful. I can post it if necessary.

  • 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-19T03:10:33+00:00Added an answer on May 19, 2026 at 3:10 am

    The type of CovariantEnum.INT_INSTANCE is CovariantEnum which returns Object from getData.

    Unfortunately, you can’t make the enum type generic either.

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

Sidebar

Related Questions

Messing around with 'extension functions' for the List module. (I spent quite a while
I have spent quite some time making a function and the last 15-20 minutes
I spent quite a bit of time modifying my Java code style settings to
I've spent quite a bit of time on this so here's where I'm stuck.
I have spent quite a few hours reading and learning about LINQ to XML , but
The question seems quite stupid, but I spent quite a lot of time trying
I'm a fairly new programmer, but I consider my google-fu quite competent and I've
I have spent quite a while trying to write a program to implement Conway's
I spent ages learning VML and creating charts for a website once, but since
I've spent most of the day making what are basically some housekeeping changes to

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.