Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does kotlin support making a class implementing an interface outside of its definition file?

Tags:

kotlin

I see kotlin.List and kotlin.MutableList is implemented by java.util.ArrayList. But where did kotlin put this trick? Compiler or somewhere in stdlib?

If kotlin supports making a class implementing an interface outside of its definition file like the ArrayList case, it will be fascinated.

like image 502
Winter Young Avatar asked Nov 16 '15 02:11

Winter Young


People also ask

Can Kotlin object implement interface?

A class in Kotlin can implement as many interfaces as they like but it can only extend from one abstract class. Properties in the interface cannot maintain state, while they can in an abstract class.

How does Kotlin implement interface function?

Interfaces in Kotlin can contain declarations of abstract methods, as well as method implementations. What makes them different from abstract classes is that interfaces cannot store a state. They can have properties, but these need to be abstract or provide accessor implementations.

Can data class implement interface Kotlin?

Android Dependency Injection using Dagger with Kotlin In Kotlin, the interface works exactly similar to Java 8, which means they can contain method implementation as well as abstract methods declaration. An interface can be implemented by a class in order to use its defined functionality.

What is difference between abstract class and interface Kotlin?

Kotlin interfaces are similar to abstract classes. However, interfaces cannot store state whereas abstract classes can. Meaning, interface may have property but it needs to be abstract or has to provide accessor implementations. Whereas, it's not mandatory for property of an abstract class to be abstract.


1 Answers

No, it is not supported.

You are right, that is only a compiler trick. There are lots of magic applied to the collections to make them right. Fortunately it is not available to the devs. Special paragraph in the docs: https://kotlinlang.org/docs/reference/java-interop.html#mapped-types

like image 134
voddan Avatar answered Nov 15 '22 08:11

voddan