Classes & Objects
Define classes with properties and methods, use constructors, understand inheritance and interfaces, and write Kotlin-idiomatic object-oriented code.
Classes & Objects is a free Android Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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 blueprintval 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 valuetoString()— human-readable outputcopy()— 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 extendedclass Dog : Animal()— extends Animaloverride 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, copycompanion object— class-level (static) membersopenclasses andoverridefor inheritanceobjectdeclarations for singletons
You're ready to build real Android apps!
Frequently asked questions
Is the “Classes & Objects” lesson free?
Yes — the full text of “Classes & Objects” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
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.