Why this is happening in Kotlin:
val list: List<Int> = listOf(1, 2, 3)// Immutable list
if(list is MutableCollection<*>){// why this "if" condition is true?
println("is mutable")// this line is printed
(list as MutableCollection<Int>).add(4) // this results to java.lang.UnsupportedOperationException
}
list is MutableCollection returns true that shows Kotlin Immutable collection objects implements MutableCollection interface but instead of changing items in collection it throws UnsupportedOperationException
Is it true? and if yes why Immutable collection objects implement MutableCollection interface in Kotlin?
Is it because of Kotlin collections inherit from Java Collections and altering methods (add, remove, ...) already is there and the only way to avoid altering collection is to override it and throw an exception (even if this is true it is not necessary for Kotlin immutable collection objects to implement MutableCollection interface because java altering collection methods already are there and can be overridden)?