Kotlin miniguide #11 Classes and constructors

In Kotlin classes are:

  • Public by default
  • Final by default

To let the coder inherit from a class we have to use the keyword open. The same applies to methods:


open class Employee {

open fun doSomething() { // can be overridden

}

}

Construction parameters are provided in the class definition. This is the primary constructor. (Internally, Kotlin provides a getter and/or a setter wheter the parameter is a val or a var)


open class Employee(val name : String) {

}

Secondary constructor:


open class Employee(val name : String) {

constructor(name: String, id : Int) : this(name)

}

Constructors and inheritance :


class Tester(name : String) : Employee(name) {



}

More about classes here

Lascia un commento