I have a Java project that uses an internal company library made in Kotlin. In this library, I have a Kotlin class that extends the Feign.Builder
. This is how the decompiled code looks like
public final class CustomizedFeign public constructor() {
public final class Builder public constructor() : feign.Feign.Builder {
...
}
}
And this is how the original Kotlin code looks like:
class CustomizedFeign {
class Builder : Feign.Builder() {
...
}
}
When I try to cast the CustomizedFeign.Builder
class to Feign.Builder
in my Java project
final Feign.Builder builder = (Feign.Builder) new CustomizedFeign.Builder();
IntelliJ shows an error:
Inconvertible types; cannot cast 'CustomizedFeign.Builder' to 'feign.Feign.Builder'
I don't get why I'm having this error. In java, you can cast from a child class to a parent class without problems. In this case, CustomizedFeign.Builder
is a subtype of Feign.Builder
, so I should be allowed to cast them... I guess the "Kotlin factor" has something to do with this error.
EDIT:
I know I shouldn't need to do an explicit cast. If I don't do it:
final Feign.Builder builder = new CustomizedFeign.Builder();
The error is different but, at the end of the day, the outcome is the same:
Required type:
feign.Feign.Builder
Provided:
CustomizedFeign.Builder
EDIT 2:
Somehow I think this is related to trying to do this in a Java project. If I try this code in a Kotlin project, I don't get any error:
val builder: feign.Feign.Builder = CustomizedFeign.Builder()
Copyright License:
Author:「Antamack」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/73590893/cast-error-on-a-kotlin-class-that-extends-a-java-class