Class (Kotlin)

A class is a blueprint for objects, bundling properties (data) and functions (behavior). Kotlin classes are concise — final and public by default — with constructors and properties declared in the header.

Kotlin

You declare a class with class Name(...) and create instances without new — just Name(args). Properties go in the primary constructor or the body, methods are functions inside the braces, and init blocks hold construction logic.

Kotlin classes are final by default, so you must mark a class open to allow inheritance and mark members open to override them — the opposite default from Java, favoring composition and explicit extensibility. Visibility defaults to public; use private, protected, or internal to restrict.

Specialized class forms cover common needs: data class for value holders, sealed class for closed hierarchies, enum class for fixed constants, and object for singletons. Plain classes are the foundation they all build on.

Syntax

class Dog(val name: String) {
    fun bark() = "$name says woof"
}

Examples

Kotlin

class Rectangle(val w: Int, val h: Int) {
    fun area() = w * h
}
val r = Rectangle(3, 4)
println(r.area())

Output:

12

Learn it in a lesson

See also

Back to the Dictionary