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

Kotlin miniguide #10 Interfaces

In Kotlin interfaces are very similar to Java interfaces, but we can also provide a default implementation for an interface member:


interface IEmployee{

var name : String //abstract property

fun doWork(){

//some default implementation

}

fun doBreak() //abstract

}

Default implementation allows us to add functionality to interfaces without breaking existing code, because default implementation is not required to be implemented:


class Tester (override var name : String) IEmployee {

   override fun doBreak () {... }

}

We can inherit from interfaces that have a member with the same signature:


interface ITester{

   fun doWork () {...}

}

class Tester (override var name : String) : ITester, IEmployee{

  override fun doBreak() {

  }

  override fun doWork() {

      super<ITester>.doWork()

     super<IEmployee>.doWork()

  }

}

Kotlin miniguide #5 Functions declaration


//typical function declaration

fun sum(a: Int, b: Int) : Int {

    return a+b

}

//function expression
fun sum(a: Int, b: Int) = a+b

//default parameters

fun payClerck(pay: Double , tip: Double = 1.0) : Double {

    return pay+ tip

}

//calling

payClerck(10.)

payClerck(10., 2.)

//using named parameters it is possible to change their

// order when calling the function

payClerck(tip = 1., pay = 2.0)

In kotlin functions are first class citizens:
A first-class citizen […] is an entity which supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, modified, and assigned to a variable. (Wikipedia)

Functions do not need to be declared inside a class.

Functions can be declared locally

Kotlin miniguide #3 Conditional

If can be used as an expression and its result assigned to a variable:


val a = 1

val b = 2

val msg = if (a > b ) "a greater than b" else "a less than b"

Kotlin doesn’t have a switch but there is something very cool called when:


val a = 1

when(a) {

    0 -> print("a is zero")

    in (1..10) ->print("a in range")

    else ->print("a not in range")

}

Kotlin miniguide #2 Compilation


//file Main.kt

class Person() {
fun greet() {
println("Hello");
}
}

fun main(args: Array) {
Person().greet()
}

In the example above I have created a file with extension kt and inside it I have added a class and the main function. In Kotlin, each class compiles to a separate .class, which gives me Person.class; plus, the main fun, which is not contained in any class, will generate a Mainkt.class

The Mainkt.class contains a public class named Mainkt with a public static method called main