Android Academy · Lesson

Classes & Objects

Define classes with properties and methods, use constructors, understand inheritance and interfaces, and write Kotlin-idiomatic object-oriented code.

Lesson 2 of 411 steps

Classes & Objects is a free Android Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

What Is a Class?

A class is a blueprint for creating objects. It bundles related data (properties) and behavior (functions) together.

  • class Dog — defines the blueprint
  • val myDog = Dog() — creates an instance (object)

Example: a User class that holds name, age, and email.

Defining a Class

Kotlin classes have a concise primary constructor syntax:

class User(val name: String, var age: Int) {
    fun greet(): String {
        return "Hi, I'm $name and I'm $age years old."
    }
}

fun main() {
    val user = User("Alice", 28)
    println(user.name)     // Alice
    println(user.age)      // 28
    user.age = 29          // var can be changed
    println(user.greet())
}

Data Classes

A data class is perfect for holding data. Add the data keyword and Kotlin auto-generates:

  • equals() / hashCode() — compare by value
  • toString() — human-readable output
  • copy() — create a modified copy

Data Class Example

Data classes shine when representing model objects:

data class Product(val id: Int, val name: String, val price: Double)

fun main() {
    val p1 = Product(1, "Laptop", 999.99)
    val p2 = p1.copy(price = 899.99) // new copy with changed price

    println(p1)           // Product(id=1, name=Laptop, price=999.99)
    println(p2)           // Product(id=1, name=Laptop, price=899.99)
    println(p1 == p2)     // false (different price)
}

Companion Objects

A companion object holds members that belong to the class itself, not to any instance — similar to static in Java.

Access them via the class name: User.create()

Companion Object Example

Use companion objects for factory methods and constants:

class Counter {
    var count = 0

    fun increment() { count++ }

    companion object {
        const val MAX = 100

        fun create(): Counter = Counter()
    }
}

fun main() {
    val c = Counter.create()
    c.increment()
    c.increment()
    println(c.count)        // 2
    println(Counter.MAX)    // 100
}

Inheritance

Kotlin classes are final by default (can't be extended). Mark a class as open to allow inheritance.

  • open class Animal — can be extended
  • class Dog : Animal() — extends Animal
  • override fun sound() — override a parent function

Inheritance Example

Build a simple class hierarchy:

open class Shape {
    open fun area(): Double = 0.0
}

class Circle(val radius: Double) : Shape() {
    override fun area(): Double = 3.14159 * radius * radius
}

class Rectangle(val w: Double, val h: Double) : Shape() {
    override fun area(): Double = w * h
}

fun main() {
    val shapes: List<Shape> = listOf(
        Circle(5.0),
        Rectangle(4.0, 3.0)
    )
    for (s in shapes) println("Area: ${s.area()}")
}

Object Declarations

An object declaration creates a singleton — a class with exactly one instance, created lazily when first accessed.

Use it for utilities, managers, or anything that should have only one instance.

Quick Check

Which feature does a Kotlin data class provide automatically?

Recap: Classes & Objects

You've learned Kotlin's OOP building blocks:

  • Classes with primary constructors and member functions
  • data class — auto-generated equals, toString, copy
  • companion object — class-level (static) members
  • open classes and override for inheritance
  • object declarations for singletons

You're ready to build real Android apps!

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “Classes & Objects” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Classes & Objects”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.

What will I learn in “Classes & Objects”?

Define classes with properties and methods, use constructors, understand inheritance and interfaces, and write Kotlin-idiomatic object-oriented code. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Classes & Objects” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Null Safety
  2. Classes & Objects
  3. Data Classes & Sealed Classes
  4. Extension Functions & Higher-Order Functions
← Back to Android Academy