Inheriting fields from a class

suggest change

Defining the base class:

open class BaseClass {
    val x = 10
}

Defining the derived class:

class DerivedClass: BaseClass() {
    fun foo() {
        println("x is equal to " + x)
    }
}

Using the subclass:

fun main(args: Array<String>) {
    val derivedClass = DerivedClass()
    derivedClass.foo() // prints: 'x is equal to 10'
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents