Difference between normal classes & data classes in Kotlin.

Dubem Ezeagwu
3 min readNov 4, 2021

Recently, I got into a bootcamp for 6 months to further my career in becoming a Software Engineer (Mobile App Development) to be precise. I decided to write a short article on what a data class & normal class in Kotlin and a few differences.

What is a class?

A class is a blueprint for an object; it shares common properties and behaviour in form of members and member functions.

In Kotlin, a class is declared with the class keyword. The class declaration consists of the class name, the class header (specifying its type parameters, the primary constructor etc.).

So technically, an object is an instance of a class.

class Person {

val name: String = "Dubem"
val age: Int = 22
fun info() = "This is $name and my age is $age"
}

fun main() {

val person = Person()
println(person)
println(person.name)
println(person.age)

}

In the example, we declare a Person class and later create an object from it.

This is the output we get when, we run the code

What is a data class?

Data class is a simple class which is used to hold data/state and contains standard functionality. A data keyword is used to declare a class as a data class. With data classes, we can considerably reduce the boilerplate code. Compiler automatically creates the equals, hashCode, toString, and copy functions.

data class User(val name: String, val email: String)

fun main() {

val u1 = User("Dubem Ezeagwu", "dubemezeagwu@gmail.com")
println(u1)

println(u1.name)
println(u1.email)

val (name, email) = u1
println("$name $email")

val u2 = User("Dubem Ezeagwu", "dubemezeagwu@gmail.com")

println(u1 == u2) // Structural equality
println(u1 === u2) // Referential equality
}
This is the output we get when, we run the code.

Differences between data classes and normal classes.

  1. A data class must be declared with at least one primary constructor parameter which must be declared with val or var. A normal class can be defined with or without a parameter in its constructor.
  2. Data classes have default implementations for the following methods using only properties that were declared in the primary constructor; toString(), hashCode(), copy(), componentN(), equals(). Implementation for those methods can be written in normal classes using properties that were and were not declared in the primary constructor.
  3. A data class cannot be extended by another class. They are final classes by default. Normal classes can be extended by other classes, including data classes. Certain conditions should however be met.
  4. Data classes cannot be sealed, open, abstract or inner. Normal classes can be any of these.

So there you have it! A brief overview of what classes & data classes are, and how they are both different in their own specific ways. I hope I have been able to explain these concepts and you have a basic understanding. Cheers!

--

--

Dubem Ezeagwu

To never try is the ultimate fail. Software Engineer. Mobile App Developer (Native Android 🤖 & Flutter 💙).