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")
}
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.
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:
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:Batman here was initialized with 5 aliases, and Superman with 6.
The output of this becomes: