Java: Is there a way to get methods with different return types in a enum?

user3133542

I know that its not possible to use generics with Enums but im searching for a way to implement a method for a enum with different return types.

For example I have following enum:

enum TestEnum {

    TEST_INTEGER() {
        @Override
        public Integer getValue() {
            return 123;
        }
    },

    TEST_STRING() {
        @Override
        public String getValue() {
            return "Test";
        }
    };

    public abstract Object getValue();
}

In this case TEST_INTEGER and TEST_STRING are implementations of TestEnum. Thats why the method getValue() has the return type Object and not Integer or String when the getValue() is invoked. That means in each case I have to cast to the concerning Classtype.

Is there any better way to get diretly the different Classtypes?

Tom Bulgur

Yes, java allows to "reduce" the return type (see here).

But in your context, this does not make sense conceptually!

Think of

TestEnum someEnum = ...
Integer val = someEnum.getValue();

How is the compiler supposed to know which one of your enum constants you actually assigned to that value?

Therefore the compiler could only understand:

 Integer val = TestEnum.TEST_INTEGER.getValue();

But well, what if you just know that it is some instance of TestEnum you are dealing with?!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java covariant return types not working for overriding methods of an enum instance?

methods that are returning different return types

Java Enum Methods - return opposite direction enum

getCode of Enum of different types in JAVA

Autofac - Resolve Service on Enum and return different types

Java: Different return type by enum

get() methods in Java enum type

Different return value types in implementation of generic methods

Refactor similair methods but with different return types

Go return values of different types in methods

Can Java methods return type Enum?

Java - Same Enum for different Types for DAO

clean way to implement similar enum types in Java

Java 8 -Two interfaces contain default methods with the same method signature but different return types, how to override?

Java class has 2 methods with the same function signature but different return types

is there a way return different types of a struct in the same function?

Why return types were different in ArrayList add methods?

Multiple Async Methods, best to use WhenAll with different return types

How do I implement abstract methods with different parameters/return types

Is this enum of two different types?

How to abstract different return types in Java?

Overriding a method with different return types in java?

Is there a way to Compare Multiple Enum Types with each other in Java

Is there a way to use functions with same names but different return types?

Is there a way to create a c++ template with different return types?

Is there a way in Java generics to return different kinds of datatype?

Putting enum types in different class

Is there an enum with MIME Types in Java?

Working with enum types in java