Skip to content

Instantly share code, notes, and snippets.

@dodyg
Last active November 21, 2022 03:05
Show Gist options
  • Save dodyg/5616605 to your computer and use it in GitHub Desktop.
Save dodyg/5616605 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.

@josueetcom
Copy link

Hey! Great examples for Kotlin! I would add a note about how the primary constructor is allowed to have optional parameters just like any other function, which usually accounts for the multiple constructors in other languages. For example, adding on to the Hero class you gave:

val somebody = Hero()
val somePaul = Hero(n = "Paul")
val highschooler = Hero(a = 16)
val father = Hero("John", 53)

class Hero (n: String = "Unknown", a : Int = -1) : Person(){
    public var name : String = n
    public var age : Int = a
}

With optional constructor arguments you get 4 constructors out of it.

Another thing to take note of is that you can also use a vararg parameter in constructors. For example, superheroes and their aliases:

class Superhero(vararg aliases: String, name: String) {
    val aliases = aliases
    val name = name
}

fun main(args: Array<String>) {
    val batman = Superhero("Bat-Man", "World's Greatest Detective", "Bruce Wayne", "Dark Knight", "Caped Crusader", name = "Batman")
    val superman = Superhero("Clark Kent", "Man of Steel", "Big Blue", "Kal-El", "Smallville", "Last Son of Krypton", name = "Superman")
    val heros = arrayOf(batman, superman)
    for (hero in heros)
        println("${hero.name} is also known as ${hero.aliases.map { it.toString() }}")
}

Batman here was initialized with 5 aliases, and Superman with 6.
The output of this becomes:

Batman is also known as [Bat-Man, World's Greatest Detective, Bruce Wayne, Dark Knight, Caped Crusader]
Superman is also known as [Clark Kent, Man of Steel, Big Blue, Kal-El, Smallville, Last Son of Krypton]

@afska
Copy link

afska commented Oct 18, 2015

Kotlin now supports secondary constructors! 😃

@dmitry-osin
Copy link

It's true

@mishaxz
Copy link

mishaxz commented Feb 23, 2016

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

are you sure about that? I thought it was public

@GiorgioNatili
Copy link

From #Kotlin documentation "A class in Kotlin can have a primary constructor and one or more secondary constructors.", I would add it to the paragraph where you say "Kotlin only allows one single constructor".

@farbodsz
Copy link

farbodsz commented Feb 2, 2017

@mishaxz I think it used to be internal visibility but it is now public by default.

@dodyg Would you be able to update the MD file?

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