Kotlin Class Inheritance Inheriting fields from a class


Example

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'
}