codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

12 / 68 topics
11Extensions Functions12Classes and Objects13Inheritance in Kotlin14Interfaces in Kotlin15Data Classes16Sealed Classes17Companion Objects18Generics in Kotlin19Type Aliases20Properties and Fields21Lateinit Properties22Delegated Properties
Tutorials/Kotlin/Classes and Objects
🎯Kotlin

Classes and Objects

Updated 2026-04-20
3 min read

Introduction

In object-oriented programming (OOP), classes and objects are fundamental concepts that form the backbone of any program. In Kotlin, these concepts are implemented with a focus on simplicity and interoperability with Java. This tutorial will provide a comprehensive guide to understanding and using classes and objects in Kotlin.

What Are Classes?

A class is a blueprint for creating objects. It defines a set of properties (data) and methods (functions) that the created objects can use. In Kotlin, you define a class using the class keyword followed by the class name.

Basic Class Definition

Here's a simple example of defining a class in Kotlin:

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

    fun greet() {
        println("Hello, my name is $name and I am $age years old.")
    }
}

In this example:

  • Person is the class name.
  • name and age are properties of the class.
  • greet() is a method that prints a greeting message.

Primary Constructor

Kotlin allows you to define a primary constructor directly in the class header. The primary constructor is part of the class header, which can include parameters:

class Person(name: String, age: Int) {
    var name: String = name
    var age: Int = age

    fun greet() {
        println("Hello, my name is $name and I am $age years old.")
    }
}

You can also initialize properties directly in the primary constructor:

class Person(var name: String, var age: Int) {
    fun greet() {
        println("Hello, my name is $name and I am $age years old.")
    }
}

Secondary Constructors

If you need additional constructors, you can define secondary constructors using the constructor keyword:

class Person(var name: String, var age: Int) {
    constructor(name: String) : this(name, 0) {
        // Additional initialization code if needed
    }

    fun greet() {
        println("Hello, my name is $name and I am $age years old.")
    }
}

What Are Objects?

An object is an instance of a class. It represents a specific implementation of the class blueprint with actual values for its properties.

Creating Objects

To create an object in Kotlin, you use the object keyword or instantiate the class using the constructor:

fun main() {
    val person1 = Person("Alice", 30)
    val person2 = Person("Bob")

    person1.greet()
    person2.greet()
}

Object Expressions

Kotlin also supports object expressions, which allow you to create anonymous objects:

fun main() {
    val calculator = object {
        fun add(a: Int, b: Int): Int {
            return a + b
        }

        fun subtract(a: Int, b: Int): Int {
            return a - b
        }
    }

    println(calculator.add(5, 3))
    println(calculator.subtract(5, 3))
}

Companion Objects

Companion objects in Kotlin are similar to static members in Java. They allow you to define properties and methods that belong to the class itself rather than instances of the class:

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

    companion object {
        const val MAX_AGE = 120

        fun createAnonymous(): Person {
            return Person("Anonymous", 0)
        }
    }

    fun greet() {
        println("Hello, my name is $name and I am $age years old.")
    }
}

fun main() {
    val anonymousPerson = Person.createAnonymous()
    println(Person.MAX_AGE)
}

Inheritance

Kotlin supports single inheritance using the : operator. A class can inherit from another class by specifying it after the colon:

open class Animal {
    open fun makeSound() {
        println("Some generic sound")
    }
}

class Dog : Animal() {
    override fun makeSound() {
        println("Woof!")
    }
}

In this example:

  • Animal is the base class.
  • Dog inherits from Animal.
  • The override keyword is used to override the makeSound() method.

Properties and Getters/Setters

Kotlin provides a concise way to define properties with automatic getters and setters:

class Person {
    var name: String = ""
        get() {
            return field.capitalize()
        }
        set(value) {
            field = value.trim()
        }

    var age: Int = 0
}

In this example:

  • The name property has a custom getter that capitalizes the name.
  • The name property also has a custom setter that trims whitespace from the input.

Data Classes

Data classes in Kotlin are used to store data. They automatically generate useful methods like equals(), hashCode(), toString(), and copy():

data class User(val name: String, val age: Int)

Usage of Data Classes

fun main() {
    val user1 = User("Alice", 30)
    val user2 = User("Bob", 25)

    println(user1) // Output: User(name=Alice, age=30)
    println(user1 == user2) // Output: false

    val user3 = user1.copy(age = 31)
    println(user3) // Output: User(name=Alice, age=31)
}

Sealed Classes

Sealed classes in Kotlin restrict the inheritance of a class to one or more specific subclasses. They are useful for representing restricted hierarchies:

sealed class Result {
    data class Success(val data: String) : Result()
    data class Error(val message: String) : Result()
}

fun handleResult(result: Result) {
    when (result) {
        is Result.Success -> println("Success: ${result.data}")
        is Result.Error -> println("Error: ${result.message}")
    }
}

Usage of Sealed Classes

fun main() {
    val success = Result.Success("Data loaded successfully")
    val error = Result.Error("Failed to load data")

    handleResult(success)
    handleResult(error)
}

Best Practices

  1. Use val for immutable properties whenever possible.
  2. Define primary constructors with default values to make your classes more flexible.
  3. Use companion objects for factory methods and constants that belong to the class.
  4. Leverage data classes for simple data-holding classes to reduce boilerplate code.
  5. Keep inheritance hierarchies shallow and well-defined.

Conclusion

Classes and objects are essential components of Kotlin programming, allowing you to create reusable and maintainable code. By understanding how to define, instantiate, and manipulate these entities, you can write robust and efficient applications in Kotlin.


PreviousExtensions FunctionsNext Inheritance in Kotlin

Recommended Gear

Extensions FunctionsInheritance in Kotlin