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

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

Data Classes

Updated 2026-05-15
10 min read

Data Classes

Introduction

In Kotlin, data classes are a special type of class designed to hold data. They automatically generate useful methods like equals(), hashCode(), toString(), and copy() based on the properties you define. This makes them particularly useful for representing data objects without having to write boilerplate code.

Concept

Data classes in Kotlin are declared using the data keyword before the class name. The primary purpose of a data class is to store data, and it should not contain complex business logic or state management. Instead, it focuses on encapsulating data attributes and providing methods for manipulating those attributes.

Key Features of Data Classes

  1. Automatic Methods: Kotlin automatically generates equals(), hashCode(), toString(), and copy() methods based on the properties defined in the class.
  2. Component Functions: For each property declared in the primary constructor, a component function is generated (e.g., component1(), component2()).
  3. Destructuring Declarations: Data classes can be used with destructuring declarations to extract values from objects easily.

Restrictions on Data Classes

  • A data class must have at least one property.
  • The primary constructor of a data class must have at least one parameter.
  • Data classes cannot inherit from other classes (except for Any), but they can implement interfaces.
  • Properties declared in the body of a data class are considered to be val by default.

Examples

Let's explore some practical examples to understand how data classes work in Kotlin.

Basic Example

Here is a simple example of a data class representing a Person.

Kotlin
1data class Person(val name: String, val age: Int)
2
3fun main() {
4 val person = Person("Alice", 30)
5 println(person) // Output: Person(name=Alice, age=30)
6}

In this example, the Person data class has two properties: name and age. The toString() method is automatically generated to provide a string representation of the object.

Copy Method

The copy() method allows you to create a copy of an existing data class instance with modified property values.

Kotlin
1data class Person(val name: String, val age: Int)
2
3fun main() {
4 val person = Person("Alice", 30)
5 val updatedPerson = person.copy(age = 31)
6 println(updatedPerson) // Output: Person(name=Alice, age=31)
7}

In this example, the copy() method is used to create a new Person object with the same name but an updated age.

Destructuring Declarations

Data classes can be used with destructuring declarations to extract values from objects easily.

Kotlin
1data class Person(val name: String, val age: Int)
2
3fun main() {
4 val person = Person("Alice", 30)
5 val (name, age) = person
6 println("Name: $name, Age: $age") // Output: Name: Alice, Age: 30
7}

In this example, the Person object is destructured into its name and age properties.

Component Functions

Component functions are generated for each property in the primary constructor. These functions can be used to access individual properties of a data class instance.

Kotlin
1data class Person(val name: String, val age: Int)
2
3fun main() {
4 val person = Person("Alice", 30)
5 println(person.component1()) // Output: Alice
6 println(person.component2()) // Output: 30
7}

In this example, component1() and component2() are used to access the name and age properties of the Person object.

What's Next?

After mastering data classes, you can explore "Sealed Classes," which provide a way to restrict class hierarchies. Sealed classes are useful for representing restricted class hierarchies where all subclasses must be declared in the same file as the sealed class itself. This tutorial will help you understand how to use sealed classes effectively in Kotlin.

Stay tuned for more tutorials on advanced topics in Kotlin!


PreviousInterfaces in KotlinNext Sealed Classes

Recommended Gear

Interfaces in KotlinSealed Classes