'How to extend Generic Kotlin class in Java?

In Kotlin I have a generic interface:

interface Repository<I : Comparable<I>> {
    fun findById(id: I): Mono<SomeClass>
}

And there is an abstract class which defines the method.

abstract class AbstractRepository: Repository<Int> {
    override fun findById(id: Int): Mono<SomeClass> {
       ...
    }
}

Then I want to extend this class in Java:

public class SomeRepository extends AbstractRepository {}

This causes the compilation error saying that SomeRepository is not abstract and does not override abstract method findById(Integer) in Repository. I understand that this is because the AbstractRepository and Repository signatures for the method are different. But how to solve the problem?



Solution 1:[1]

You can override and call through to the super-class implementation that is using the unboxed int:

public class SomeRepository extends AbstractRepository {
    
    @NotNull
    public Mono<SomeClass> findById(@NotNull Integer id) {
        return findById(id.intValue());
    }
    
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tenfour04