Cast error on a Kotlin class that extends a Java class

2022-09-03T16:53:08

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

About “Cast error on a Kotlin class that extends a Java class” questions

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 ...
I am experimenting with Kotlin on Android, with Android Studio, mixing it with some existing Java code, and I've come across a problem. I have a base class in Kotlin with a single method marked as
I have error when try compile my java code with kotlin dependencies: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'functionBindingRegistrar' defined in class
How to convert this Kotlin class to Java sealed class RepoSearchResult { data class Success(val data: List<Repo>) : RepoSearchResult() data class Error(val error: Exception) :
How to convert this Kotlin class to Java sealed class RepoSearchResult { data class Success(val data: List<Repo>) : RepoSearchResult() data class Error(val error: Exception) :
I have a Java pojo class like this: public class Event { private final List<String> urls; private final int totalPhotoCount; private final Owner owner; public Event(@NonNull...
I get a class with Class.forName("classname").kotlin. I know that the class implements an Interface, which is extremly simple (if you have a solution that is different from what I think, it would ...
Kotlin compiler is giving me error while running below : fun main() { val x = object { val i = 1 val k = "s" } val y = x as Planet if (y is Planet) {
I am writing an extension function for RecyclerView to set LayoutManager and Adapter to the RecyclerView.The function should accept any Adapter class which is Extending from RecyclerView.Adapter&lt...
I am trying to access public attributes and public functions in java class, inside a kotlin class. Whenever I try to REPL it using android studio's kotlin REPL, it throws a stub or AndroidManifest ...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.