Cast error on a Kotlin class that extends a Java class

Issue

This Content is from Stack Overflow. Question asked by Antamack

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 {

    fun builder(): Builder = Builder()

    class Builder : Feign.Builder() {
       ... 
    }
}

When I try to cast the CustomizedFeign 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 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.



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?