Kotlin Tutorial menu

Kotlin Classes and Objects

A class is a blueprint, and an object is a real thing made from that blueprint.

What is a Class?

Think of a student admission form. The form is the class: it says every student has a name and marks. Each filled form is an object: Ali with 85 marks, Ayesha with 92 marks.

WordMeaning
ClassThe blueprint (Student)
ObjectOne instance of the class (Ali)
PropertyData inside the object (name, marks)
MethodA function inside the class (printInfo)

Creating a Class

In Kotlin you can declare properties directly in the brackets after the class name. This is the primary constructor.

Example

kotlin
class Student(val name: String, var marks: Int) {
    fun printInfo() {
        println("$name has $marks marks")
    }

    fun hasPassed(): Boolean = marks >= 50
}

fun main() {
    val ali = Student("Ali", 85)
    val sara = Student("Sara", 40)

    ali.printInfo()
    sara.printInfo()
    println(sara.hasPassed())

    sara.marks = 55
    println(sara.hasPassed())
}
Output
Ali has 85 marks
Sara has 40 marks
false
true

There is no new keyword in Kotlin. You create an object by calling the class like a function.

The init Block

Code inside init runs every time an object is created. It is useful for checks:

Example

kotlin
class Product(val name: String, val price: Int) {
    init {
        println("Created $name for Rs $price")
    }
}

fun main() {
    val pen = Product("Pen", 50)
    val bag = Product("School bag", 1800)
}
Output
Created Pen for Rs 50
Created School bag for Rs 1800

Inheritance

A class can inherit properties and methods from another class. In Kotlin, classes are closed by default, so the parent must be marked open. A method that a child changes must also be open, and the child uses override.

Example

kotlin
open class Vehicle(val name: String) {
    open fun sound() {
        println("$name makes a sound")
    }
}

class Rickshaw(name: String) : Vehicle(name) {
    override fun sound() {
        println("$name goes put-put-put")
    }
}

class Bus(name: String) : Vehicle(name)

fun main() {
    Rickshaw("Rickshaw").sound()
    Bus("Green Line bus").sound()
}
Output
Rickshaw goes put-put-put
Green Line bus makes a sound

Bus did not override sound(), so it uses the version from Vehicle.

Data Classes

Many classes only hold data. Add data before class, and Kotlin writes useful functions for you: a readable toString(), == that compares values, and copy().

Example

kotlin
data class Book(val title: String, val price: Int)

fun main() {
    val b1 = Book("Kotlin Basics", 900)
    val b2 = Book("Kotlin Basics", 900)
    println(b1)
    println(b1 == b2)

    val sale = b1.copy(price = 700)
    println(sale)
}
Output
Book(title=Kotlin Basics, price=900)
true
Book(title=Kotlin Basics, price=700)

Objects (Singletons)

When you need exactly one instance, use object instead of class:

Example

kotlin
object Settings {
    var darkMode = false
}

fun main() {
    Settings.darkMode = true
    println(Settings.darkMode)
}
Output
true

Exercise

Exercise

What must you add to a Kotlin class so that other classes can inherit from it?

Learn Kotlin with a teacher

Join the App Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp