Kotlin Classes and Objects Tutorial: Class Declaration, Constructors, Properties, and Object Creation


This Kotlin Classes and Objects tutorial explains how to use object-oriented programming concepts in Kotlin. It covers class declaration, primary and secondary constructors, properties, the init block, and object creation. Each topic includes practical examples and best practices to help beginners and professionals build modular, reusable, and maintainable Kotlin programs using Kotlin’s object-oriented features.

Kotlin Classes and Objects – Complete Tutorial

Class Declaration

In Kotlin, a class is a blueprint for objects. A simple class can be declared using the class keyword.

Syntax


class Person {
// properties and methods
}

Example


class Person {
var name: String = ""
var age: Int = 0
}

Best Practices

  1. Use PascalCase for class names.
  2. Keep each class focused on a single responsibility.

Primary Constructor

The primary constructor is part of the class header and allows initializing properties when an object is created.

Syntax


class Person(val name: String, var age: Int)

Example


fun main() {
val person = Person("Muni", 35)
println("Name: ${person.name}, Age: ${person.age}")
}

Best Practices

  1. Use primary constructors for required properties.
  2. Keep constructors concise.

Secondary Constructor

A secondary constructor is defined inside the class using constructor keyword. It allows multiple ways to create objects.

Example


class Person {
var name: String
var age: Int

constructor(name: String, age: Int) {
this.name = name
this.age = age
}

constructor(name: String) {
this.name = name
this.age = 0
}
}

fun main() {
val person1 = Person("Muni", 35)
val person2 = Person("Ravi")
println("${person1.name}, ${person1.age}")
println("${person2.name}, ${person2.age}")
}

Best Practices

  1. Use secondary constructors for optional parameters or alternative initialization.

Properties

Properties are variables declared inside the class. They can have default values and custom getters/setters.

Example


class Person {
var name: String = ""
var age: Int = 0
set(value) {
if (value >= 0) field = value
}
}

Best Practices

  1. Use val for read-only properties and var for mutable ones.
  2. Use custom getters/setters to enforce data validation.

init Block

The init block is executed when an object is created. It is useful for initialization logic.

Example


class Person(val name: String, var age: Int) {
init {
println("Person created: $name, $age years old")
}
}

fun main() {
val person = Person("Muni", 35)
}

Best Practices

  1. Use init block for initialization logic that cannot be done in property declarations.
  2. Keep init blocks minimal and focused.

Object Creation

Objects are instances of a class. After declaring a class, you can create objects using the class constructor.

Example


fun main() {
val person = Person("Muni", 35)
println("Name: ${person.name}, Age: ${person.age}")
}

Best Practices

  1. Use meaningful object names.
  2. Avoid creating unnecessary objects.

Summary

This chapter introduced object-oriented programming in Kotlin, covering class declaration, primary and secondary constructors, properties, init blocks, and object creation. Mastering these concepts is essential for writing modular, reusable, and maintainable Kotlin programs.