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

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...