Skip to content

Instantly share code, notes, and snippets.

@vmlinz
Forked from dodyg/gist:5616605
Created May 16, 2016 15:48
Show Gist options
  • Save vmlinz/98c4adceb5b1a9121c71b3a62f93b4a4 to your computer and use it in GitHub Desktop.
Save vmlinz/98c4adceb5b1a9121c71b3a62f93b4a4 to your computer and use it in GitHub Desktop.
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)
  • Secondary constructors
  • 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## Unlike many other OO language, Kotlin only allows one single 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment