Generic type in interface method Java

pavlos163

I have this method in my interface:

  public String getValue();

String is just for the example here, I don't want to return a String.

And I want to put the method in classes that will return ints, chars or bools (depending on the class). For example, I have a class Animal that implements my interface and has this method. I want it to return an int when this method is invoked in an animal. I have another class person that has this method, and when this method is invoked on a Person I want it to return a char.

public class Animal implements myInterface {
  @Override
  public int getValue() {
    return 5;
  }
}

public class Person implements myInterface {
  @Override
  public char getValue() {
    return 'c';
  }
}

I understand I have to do this with generics. But how exactly do I do it?

If I replace String in the interface with a generic type, like:

public <T> T getValue();

then it says I cannot cast from int (or char) to T.

What should I do? Thanks.

Tassos Bassoukos

The proper way to do that is by having a generic interface:

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

public class Animal implements MyInterface<Integer> {
  @Override
  public Integer getValue() {
    return 5;
  }
}

However, this really reeks like an X/Y problem; not to mention that it's highly inefficient, boxing and unboxing all those primitives.

What are you trying to achieve? What is your goal?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Interface with the method of generic type

From Dev

Generic type constraints on an interface method

From Dev

Java how to implement interface with a variadic method and generic return type

From Dev

Specialize type in method that implements a generic interface method

From Dev

Invoke generic method by type where type is interface

From Dev

Override java generic method in interface

From Dev

Method parameter: Interface VS Generic type

From Dev

Generic method with return type from interface

From Dev

How does one define a generic wrapper interface type and use it as method return type in java?

From Dev

Java generic method type argument

From Dev

Java generic method type argument

From Dev

java generic method return type

From Dev

Java - How to define interface method with generic

From Dev

ScalaMock mocking generic Java interface overloaded method

From Dev

Implementing generic method from an interface in Java

From Dev

Java method that returns interface type

From Dev

Java method that returns interface type

From Dev

Java instantiating a generic type which implements an interface

From Dev

Why a generic method of an interface can be implemented as non-generic in Java?

From Dev

Why a generic method of an interface can be implemented as non-generic in Java?

From Dev

Can the generic type of a generic Java method be used to enforce the type of arguments?

From Dev

Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

From Dev

C# - Returning an implementation when generic method type is an Interface

From Dev

How to use generic function interface method type parameters in a lambda

From Dev

Calling Type.GetRuntimeMethod on an interface with a generic method returns null

From Dev

C# - Interface generic method constraint to match derived type

From Dev

how to write a java method with generic class type?

From Dev

Confusion over Java generic method type inference

From Dev

Java Reflection, extract generic type from method

Related Related

  1. 1

    Interface with the method of generic type

  2. 2

    Generic type constraints on an interface method

  3. 3

    Java how to implement interface with a variadic method and generic return type

  4. 4

    Specialize type in method that implements a generic interface method

  5. 5

    Invoke generic method by type where type is interface

  6. 6

    Override java generic method in interface

  7. 7

    Method parameter: Interface VS Generic type

  8. 8

    Generic method with return type from interface

  9. 9

    How does one define a generic wrapper interface type and use it as method return type in java?

  10. 10

    Java generic method type argument

  11. 11

    Java generic method type argument

  12. 12

    java generic method return type

  13. 13

    Java - How to define interface method with generic

  14. 14

    ScalaMock mocking generic Java interface overloaded method

  15. 15

    Implementing generic method from an interface in Java

  16. 16

    Java method that returns interface type

  17. 17

    Java method that returns interface type

  18. 18

    Java instantiating a generic type which implements an interface

  19. 19

    Why a generic method of an interface can be implemented as non-generic in Java?

  20. 20

    Why a generic method of an interface can be implemented as non-generic in Java?

  21. 21

    Can the generic type of a generic Java method be used to enforce the type of arguments?

  22. 22

    Why can't I extend an interface "generic method" and narrow its type to my inherited interface "class generic"?

  23. 23

    C# - Returning an implementation when generic method type is an Interface

  24. 24

    How to use generic function interface method type parameters in a lambda

  25. 25

    Calling Type.GetRuntimeMethod on an interface with a generic method returns null

  26. 26

    C# - Interface generic method constraint to match derived type

  27. 27

    how to write a java method with generic class type?

  28. 28

    Confusion over Java generic method type inference

  29. 29

    Java Reflection, extract generic type from method

HotTag

Archive