Thuta Learning
IntermediateProgrammingbeginner

Classes & Objects

Relax. We'll talk through this in plain words — no textbook voice.

A class is the blueprint for an object. It can contain properties that store data and functions that do the work. In real projects, entities like User, Product, Order, and Course are usually built with classes.

kotlin
class Customer {
    var name = ""

    fun printName() {
        println("Customer name is $name")
    }
}

fun main() {
    val customer = Customer()
    customer.name = "Kyaw"
    customer.printName()
}
You should see
Customer name is Kyaw

Summary

A class neatly bundles data and behavior together in one place.

Easy traps

  • Just declaring a class doesn't create an object yet. You need to create an instance, like Customer(), before you can use it.

Practical example — product object

Practical example — product object

kotlin
class Product {
    var name = ""
    var price = 0

    fun showInfo() {
        println("$name - $price MMK")
    }
}

fun main() {
    val product = Product()
    product.name = "Keyboard"
    product.price = 45000
    product.showInfo()
}

You'll know it worked when: Keyboard - 45000 MMK