Why an kotlin.collections.ArrayList can be assigned to kotlin.collections.List property?


#1

Can please anyone explain me this thing:

val list : kotlin.collections.List<Int> = kotlin.collections.ArrayList() // compiled successfully

Why this statement is compiled successfully in Kotlin?

The kotlin.collections.ArrayList type is a typealias for java.util.ArrayList. But java.util.ArrayList class does not implement kotlin.collections.List interface.


#2

This is an implementation issue. How java.util.ArrayList can implement kotlin List if it doesn’t know about it?


#3

java.util.ArrayList implements java.util.List. MutableList contains (a subset of) functions also found in java.util.List The JVM target compiler transparently maps java.util.List to MutableList, and vice versa.

As List is a supertype of MutableList, you can assign any MutableList to List.