Kotlin — Classes
Basic class
class Person(var name: String, var age: Int) {
fun greet() {
println("Hi, I'm $name!")
}
}
fun main() {
val alice = Person("Alice", 30)
alice.greet()
alice.age = 31
}
Primary constructor
class Person(
val name: String,
val age: Int,
val email: String = "unknown"
)
fun main() {
val p = Person("Alice", 30, "alice@example.com")
println("${p.name}, ${p.age}")
}
Init block
class Person(val name: String) {
init {
require(name.isNotEmpty()) { "Name must not be empty" }
println("Person created: $name")
}
}
fun main() {
val p = Person("Alice")
}
Properties
class Temperature {
var celsius: Double = 0.0
set(value) {
require(value >= -273.15) { "Below absolute zero" }
field = value
}
val fahrenheit: Double
get() = celsius * 9 / 5 + 32
}
fun main() {
val t = Temperature()
t.celsius = 100.0
println("${t.celsius}°C = ${t.fahrenheit}°F")
}
Inheritance
open class Animal(val name: String) {
open fun speak() = println("$name makes a sound")
}
class Dog(name: String) : Animal(name) {
override fun speak() = println("$name says Woof!")
}
class Cat(name: String) : Animal(name) {
override fun speak() = println("$name says Meow!")
}
fun main() {
val animals = listOf(Dog("Rex"), Cat("Whiskers"))
animals.forEach { it.speak() }
}
Data classes
data class Point(val x: Double, val y: Double)
fun main() {
val p1 = Point(3.0, 4.0)
val p2 = Point(3.0, 4.0)
println(p1) // Point(x=3.0, y=4.0)
println(p1 == p2) // true
println(p1.copy(y = 5.0)) // Point(x=3.0, y=5.0)
val (x, y) = p1
println("x=$x, y=$y")
}
Sealed classes
sealed class Result {
data class Success(val data: String) : Result()
data class Error(val message: String) : Result()
}
fun handle(result: Result) {
when (result) {
is Result.Success -> println("Data: ${result.data}")
is Result.Error -> println("Error: ${result.message}")
}
}
fun main() {
handle(Result.Success("Hello"))
handle(Result.Error("Something went wrong"))
}
Companion objects
class User private constructor(val name: String) {
companion object {
fun create(name: String): User {
println("Creating user: $name")
return User(name)
}
}
}
fun main() {
val user = User.create("Alice")
println(user.name)
}
Mini Practice
Write Kotlin code that:
- Creates a class with primary constructor and init block
- Uses a data class with copy and destructuring
- Creates an open class and overrides a method
- Uses a sealed class with
when
Up Next
In the next lesson, you'll learn about Constructors — initialization patterns in Kotlin.
Related Topics
Frequently Asked Questions about Classes
What is Classes in Kotlin?
Classes is a fundamental concept in Kotlin. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Classes?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Classes.
Why is Classes important in Kotlin?
Classes is essential for Kotlin development. Understanding this concept will help you write better code and solve real-world problems more effectively.