Tsallaka zuwa abun ciki
Justice Tech School

Kotlin Classes and Objects

A class has properties and functions. The primary constructor sits in the class header, and val or var there creates properties. No new keyword is needed.

Example
class Student(val name: String, var score: Int = 0) {
    fun add(p: Int) { score += p }
    fun describe() = "$name: $score"
}
val s = Student("Ada", 70)
s.add(5)
println(s.describe())

Exercise

Declare a class.

 Dog(val name: String)

Quiz Test

Do you use new in Kotlin?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.