Skip to content
Advertisement

Java returning Generic Type problem: unchecked overriding return type requires unchecked conversion

Hi I am having problems with Generic Types and warning that I get from IDE. I have class that holds generic type value:

JavaScript

I have abstract class:

JavaScript

and class that extends it:

JavaScript

IDE is giving me warning: unchecked overriding return type requires unchecked conversion. What I am missing?

Advertisement

Answer

JavaScript

This is not the promise you intended to make. This says “for every T, I promise I can get a source of type SourceFunction<T>.” Then you came along and subclassed it with

JavaScript

Which says “specifically, I can get you a source of type Result<MyEvent>“, a much weaker promise.

If you intend that getSource only return one specific type, your generic should be on the CustomSource class, not the getSource function.

JavaScript

Then it makes sense to use OtherCustomSource as a CustomSource<Result<MyEvent>> (since it can get a source of type Result<MyEvent>), but it makes no sense to treat a OtherCustomSource as a CustomSource<Integer>, for example.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement