Is it possible to have an interface method defined with a generic return type and a concrete implementation define the return type?

You probably want to change the interface to: public interface OperandValue { T getValue(); } And the implementation to: public class NumberOperandValue implements OperandValue { @Override public Integer getValue() { // some integer value that is set elsewhere return 1; } } Now you're telling the interface what type you want that method to return. In other words, you're making the interface type generic, rather than the method declaration. But that seems to be what you want As a side note: public Integer getValue() Actually means, 'define a generic type parameter with the name "Integer"' where getValue returns the "Integer" type that you just defined Responding To Steve's Comments Below : when I remove the Integer from my method implementation that I then get a warning that says: Type safety: The return type Integer for getValue() from the type NumberOperandValue needs unchecked conversion to conform to T from the type OperandValue That warning message indicates that you're breaking the rules when using Java generics.To see why, let's consider what the method signature means before you remove the Integer type parameter public Integer getValue() This signature means that the method getValue returns a value of type Integer where Integer is defined as the generic type parameter you defined between the angle brackets.

The meaning of the string Integer is completely arbitrary and would have exactly the same meaning as: public T getValue() For clarity, let's stick with this version of your method signature for the purposes of your question. What happens when we remove the type parameter? Public T getValue() Now if you were to try to compile, you'd get an error that T is undefined.

However, because your original type signature declared the type parameter with the name Integer when you removed it, you were left with: public Integer getValue() Because Integer is already a predefined type, the method signature is still technically legal. However, it is only an accident that the name of your type parameter happens to be the same as a type that already exists Furthermore, because your interface already declared a method signature with generics, the Java compiler generates a warning when you remove it from the implementation. Specifically, the compiler is concerned that in the base class, the return type of the method is the (type-erased to Object ) generic parameter named Integer which is not the same type (nor is it known to be type compatible with) the system class named Integer (or java.lang.

Inteeger to be precise).

You probably want to change the interface to: public interface OperandValue { T getValue(); } And the implementation to: public class NumberOperandValue implements OperandValue { @Override public Integer getValue() { // some integer value that is set elsewhere return 1; } } Now you're telling the interface what type you want that method to return. In other words, you're making the interface type generic, rather than the method declaration. But that seems to be what you want.As a side note: public Integer getValue() Actually means, 'define a generic type parameter with the name "Integer"' where getValue returns the "Integer" type that you just defined.

Responding To Steve's Comments Below: when I remove the from my method implementation that I then get a warning that says: Type safety: The return type Integer for getValue() from the type NumberOperandValue needs unchecked conversion to conform to T from the type OperandValue That warning message indicates that you're breaking the rules when using Java generics. To see why, let's consider what the method signature means before you remove the type parameter. Public Integer getValue() This signature means that the method getValue returns a value of type Integer where Integer is defined as the generic type parameter you defined between the angle brackets.

The meaning of the string Integer is completely arbitrary and would have exactly the same meaning as: public T getValue() For clarity, let's stick with this version of your method signature for the purposes of your question. What happens when we remove the type parameter? Public T getValue() Now if you were to try to compile, you'd get an error that T is undefined.

However, because your original type signature declared the type parameter with the name Integer, when you removed it, you were left with: public Integer getValue() Because Integer is already a predefined type, the method signature is still technically legal. However, it is only an accident that the name of your type parameter happens to be the same as a type that already exists. Furthermore, because your interface already declared a method signature with generics, the Java compiler generates a warning when you remove it from the implementation.

Specifically, the compiler is concerned that in the base class, the return type of the method is the (type-erased to Object) generic parameter named Integer, which is not the same type (nor is it known to be type compatible with) the system class named Integer (or java.lang. Inteeger to be precise).

– Kal Sep 15 at 15:39 @Kal, yes, it's possible. But it's not possible to "plug in" the specific type in an implementation class. If the method is generic that means the caller of the method is responsible for providing the type argument.

– Kirk Woll Sep 15 at 15:49 Thanks Kirk. I see. If the interface definition was as defined by the OP, then the implementation should have the method defined as @Override public T getValue() also.

Correct? – Kal Sep 15 at 16:02 @Kal, yes, that's correct. – Kirk Woll Sep 15 at 16:51 @KirkWoll, thanks for your response.

I understand the naming issue now. Sticking with the method level generic for a moment, can you help me understand why it is that when I remove the from my method implementation that I then get a warning that says: Type safety: The return type Integer for getValue() from the type NumberOperandValue needs unchecked conversion to conform to T from the type OperandValue? – Steve Sep 15 at 17:24.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions