Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Classes and Instances

Classes and Instances

In this lesson — part of Objects and Classes — you'll learn classes and instances in Kotlin and why it matters in real work.

Why it matters

Classes bundle data and behavior together — the core of object-oriented design.

Key ideas

  • Classes and instances
  • Fields and methods
  • Constructors
  • Encapsulation

In practice

Here's how it looks in idiomatic Kotlin:

class Counter(private var value: Int = 0) {
    fun increment() { value++ }
    fun get(): Int = value
}

fun main() {
    val c = Counter()
    c.increment()
    println(c.get())   // 1 — no 'new' keyword
}

Kotlin note: The primary constructor lives in the class header (class Counter(...)), and you instantiate by calling the class like a function — Kotlin has no new.

Try it yourself

Exercise: In Kotlin, write a Bank Account class with deposit and withdraw methods.

Recap

You now understand classes and instances and can apply it in Kotlin. Mark this lesson complete and continue to the next one.