Skip to content
Learn Netverks

Lesson

Step 12/36 33% through track

classes-kotlin

Classes in Kotlin

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~2 min
Level
beginner

This lesson

This lesson teaches Classes in Kotlin: the syntax, patterns, and safety habits you need before advancing in Kotlin.

Teams still ship Classes in Kotlin in Kotlin codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Classes in Kotlin in contexts like: Android apps, Spring services, and shared KMP modules.

Write Kotlin in main.kt with fun main(), click Run on server—the dev runner kotlinc compiles to a JVM jar and java runs it; use println for output (requires JDK + kotlinc; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

A class in Kotlin groups state and behavior like Java classes, but properties replace many getter/setter pairs and constructors are concise. Instances are created without new—just call the constructor like a function.

Properties and constructors

class User(val name: String, var score: Int = 0) {
    fun greet() = "Hi, $name"
}

val ada = User("Ada")
println(ada.greet())

Primary constructor parameters in the header can declare val or var properties automatically. Use an init { } block for validation logic.

Compared to Java and C#

Unlike C# auto-properties declared separately, Kotlin often declares them in the constructor header. Everything is reference-typed on the JVM like Java—no new keyword.

Important interview questions and answers

  1. Q: Class vs object in Kotlin?
    A: Class is the blueprint; object is a heap instance—same JVM model as Java, with lighter syntax.
  2. Q: val vs var on class properties?
    A: val exposes a read-only property; var allows reassignment after construction.

Self-check

  1. How do you create an instance without new?
  2. Where can you put validation in a class?

Tip: Classes are final by default—use open only when inheritance is intentional.

Interview prep

new keyword?

Kotlin omits new—call constructors like functions.

final by default?

Classes are final unless marked open.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Primary constructor?
  • init block when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump