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
- Q: Class vs object in Kotlin?
A: Class is the blueprint; object is a heap instance—same JVM model as Java, with lighter syntax. - Q: val vs var on class properties?
A:valexposes a read-only property;varallows reassignment after construction.
Self-check
- How do you create an instance without
new? - 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.