Question from the Kotlin test

In Kotlin, how to override a property in a subclass and call the superclass's property in the constructor?

Medium
open class Base(val name: String) {

    init { println("Initializing Base") }

    open val size: Int =
        name.length.also { println("Initializing size in Base: $it") }
}

class Derived(
    name: String,
    val lastName: String
    ) : Base(name.capitalize().also { println("Argument for Base: $it") }) {

    init { println("Initializing Derived") }

    override val size: Int =
        (super.size + lastName.length).also { println("Initializing size in Derived: $it") }
}

Executingval derived= Derived("jean","Dupont"), outputs:

Author: ahmedStatus: Published(Update)Question passed 77 times
Edit
0
Community EvaluationsNo one has reviewed this question yet, be the first!