Hi all, I have a java class with a method I want t...
# announcements
t
Hi all, I have a java class with a method I want to override in my kotlin code. The method returns java.lang.Object, meaning it can possibly be null, and I would like to return null from my kotlin implementation. The problem is that I am not allowed to specify the return type of my method as
Any?
when the method I'm overloading has return type
Object
🤔
d
I just tried this out and it works fine in the way you described (specifying the return type as
Any?
in the override). Are you sure there are no
@NonNull
, etc annotations on the Java side?
t
how strange. There's no
@NonNull
annotations
I'm still on Kotlin 1.3.70, are you using 1.4?
d
yes, but that shouldn't matter. This behavior has existed since the very early days of Kotlin
t
Return type of 'convert' is not a subtype of the return type of the overridden member 'public open fun convert(p0: ByteArray): Any defined in org.springframework.core.serializer.support.DeserializingConverter'
I'll try to make a minimal reproduction
the
@NonNull
annotations are at the package level
t
oh, I didn't know that
the method I'm trying to override has an explicit
@Nullable
annotation, but I'm not sure how that interacts with the package level annotations
But I'm inheriting from DeserializingConverter, and that method has no annotations, so it sort of makes sense
r
I would say, there is not much you can do about this if it doesn't compile 🙂
t
seems it can be worked around by using delegation instead of inheritance
👍 1
in my case 🙂
thank you both