Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kotlin Programming Language Cheat Sheet Part 2

This is a quick guide to Kotlin programming language. The previous part of this guide is here

Object Oriented

fun main(args : Array<String>) {
  class local (val x : Int)
  
  val y = local(10)
  println("${y.x}")
}

Above code is a sample of Local Class, one of many support that Kotlin has for OO programming.

  • Abstract classes
  • Primary constructor
  • Delegation
  • Generic classes
  • Class objects
  • Nested classes
  • Local classes
  • Object expressions
  • Traits
  • Data classes
  • Anonymous Analyzer
  • Anonymous Objects

Kotlin classes

Kotlin classes does not have:

  • Static member (methods or properties)
  • No fields, just properties

Simplest Kotlin class definition

class Person


fun main(args : Array<String>) {
  val p = Person()
  val name = javaClass<Person>().getSimpleName()
  println("$name")
}

The class Person is as simple as you can get to declare a class

by default, a Kotlin class is final. So to make a class inheritable, you must you the keyword open in front of it

open class Person
class Hero : Person()


fun main(args : Array<String>) {
  val name = javaClass<Person>().getSimpleName()
  println("$name")
  
  val name2 = javaClass<Hero>().getSimpleName()
  println("$name2")
}

Visibilities

Kotlin has four visibilities:

  • private
  • protected
  • internal
  • public

If you do not declare a visibility modifier, it is assumed to be internal visibility.

fun main(args : Array<String>) {
    val x = Visibility()
}

class Visibility{
 public var name : String = ""
 private var age : Int = 0
 protected var address : String = ""
 internal var friends : String = ""  
 var status : String = "Single"
}

An empty class is off course useless. Let's add some properties to it so it can hold data

open class Person
  
class Hero : Person(){
  public var name : String = ""
  public var age : Int = 30
  }


fun main(args : Array<String>) {
  val h = Hero()
  h.name = "Superman"
  h.age = 30
  
  println("${h.name} is ${h.age} years old")
}

Rule

  • Every declared property must be initialized, without exception.
  • a var property means it can be modified
  • a val property is a constant

Primary constructor

Primary constructor

open class Person
  
class Hero (n: String, a : Int) : Person(){
  public var name : String = n
  public var age : Int = a
  }


fun main(args : Array<String>) {
  val h = Hero("Superman", 30)
  println("${h.name} is ${h.age} years old")
}

As you can see, the constructor parameter n and a are being used to initialized their respective properties.

Secondary constuctors

https://kotlinlang.org/docs/reference/classes.html#secondary-constructors

The class can also declare secondary constructors, which are prefixed with constructor:

class Person {
    constructor(parent: Person) {
        parent.children.add(this)
    }
}

If the class has a primary constructor, each secondary constructor needs to delegate to the primary constructor, either directly or indirectly through another secondary constructor(s). Delegation to another constructor of the same class is done using the this keyword:

class Person(val name: String) {
    constructor(name: String, parent: Person) : this(name) {
        parent.children.add(this)
    }
}

If a non-abstract class does not declare any constructors (primary or secondary), it will have a generated primary constructor with no arguments. The visibility of the constructor will be public. If you do not want your class to have a public constructor, you need to declare an empty primary constructor with non-default visibility:

class DontCreateMe private constructor () {
}

NOTE: On the JVM, if all of the parameters of the primary constructor have default values, the compiler will generate an additional parameterless constructor which will use the default values. This makes it easier to use Kotlin with libraries such as Jackson or JPA that create class instances through parameterless constructors.

class Customer(val customerName: String = "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.