Long Quanzheng
Long Quanzheng

Reputation: 2391

How do I extend a Java generic interface with extends key words in Kotlin

If I have a java class like this:

public interface MyInterface<E extends Apple, EE extends Banana> {
    Class<E> foo();

    @Nullable
    EE bar(E e);
}

Upvotes: 0

Views: 76

Answers (1)

szymon_prz
szymon_prz

Reputation: 560

If you have some implementations of Apple and Banana, then you just need to specify them as generics.

For example if i have such implementations of Apple and Banana in Java

public class AppleJavaImpl extends Apple {}

public class BananaJavaImpl extends Banana {}

then you can implement your Java MyInterface in Kotlin

class MyJavaInterfaceImplementationInKotlin: MyInterface<AppleJavaImpl, BananaJavaImpl> {
    override fun foo(): Class<AppleJavaImpl> {
        TODO("Not yet implemented")
    }

    override fun bar(e: AppleJavaImpl?): BananaJavaImpl? {
        TODO("Not yet implemented")
    }
}

the same applies with kotlin implementations of Apple and Banana

class AppleKotlinImpl: Apple()

class BananaKotlinImpl: Banana()

class MyJavaInterfaceImplementationInKotlin: MyInterface<AppleKotlinImpl, BananaKotlinImpl> {
    override fun foo(): Class<AppleKotlinImpl> {
        TODO("Not yet implemented")
    }

    override fun bar(e: AppleKotlinImpl?): BananaKotlinImpl? {
        TODO("Not yet implemented")
    }
}

Upvotes: 1

Related Questions