Design Converter
Education
Software Development Executive - II
Last updated on Dec 11, 2024
Last updated on Dec 10, 2024
When working with Kotlin, especially in domains like mobile app development, you’ll frequently encounter the need to create classes. However, there’s often confusion about when to use a normal class versus a data class.
This blog will provide a clear, technical comparison of the two, ensuring you have a basic understanding of their differences and use cases.
In Kotlin, a class is a blueprint used to define objects. You can define classes with the class keyword and add functionality or properties to them. A normal class can be used to represent any entity and may include methods, variables, and other elements.
1class Person(val name: String, val age: Int) { 2 fun introduce() { 3 println("Hi, my name is $name and I am $age years old.") 4 } 5} 6 7fun main() { 8 val person = Person("Alice", 25) 9 person.introduce() 10}
Here, the Person class has a primary constructor with primary constructor parameters name and age. You can add more functionality in the class body.
• Normal classes require manual implementation of boilerplate code like toString, equals, or hashCode if needed.
• They are versatile and not limited to holding data.
• Developers can include any behavior or logic, including the ability to implement interfaces or define a secondary constructor.
A data class is a special type of class in Kotlin designed specifically to hold data. It minimizes boilerplate code by providing automatically generated functions for common tasks like equality checks, copy operations, and a readable string representation. A data class declaration is simple and compact, yet it meets the following requirements:
Must include at least one parameter in the primary constructor.
The parameters in the primary constructor should be marked as val or var.
Cannot be abstract, sealed, or inner.
1data class Person(val name: String, val age: Int) 2 3fun main() { 4 val person1 = Person("Bob", 30) 5 val person2 = Person("Bob", 30) 6 7 println(person1) // String representation: Person(name=Bob, age=30) 8 println(person1 == person2) // Equality check: true 9}
This example demonstrates how the compiler automatically derives the toString, equals, and hashCode functions. Unlike a normal class, you don’t need to manually implement these functions.
• Designed to hold data rather than behavior.
• Include following members: toString, hashCode, equals, and copy.
• Ideal for immutable objects and scenarios requiring default values or data transfer between layers.
• A normal class can be used to define entities with behavior, logic, and custom methods.
• A data class focuses on storing and processing data with minimal code.
• In a normal class, you write methods like toString or equals manually.
• A data class comes with automatically generated functions like copy, toString, and equals.
• Normal classes offer no standard functionality unless explicitly defined.
• Data classes in Kotlin provide out-of-the-box features like equality checks and readable output.
• Use a normal class for complex logic or where custom behavior is needed.
• Use a data class for simple objects that hold data like data class person or data class user.
One of the standout features of a data class is the copy function. This function allows you to create a new object with some modified properties, while leaving the rest unchanged.
1data class User(val id: Int, val name: String, val age: Int) 2 3fun main() { 4 val user1 = User(1, "Charlie", 28) 5 val user2 = user1.copy(name = "Dave") 6 println(user2) // User(id=1, name=Dave, age=28) 7}
Here, the copy function creates a new object with the name property modified.
Equality checks between two objects behave differently for normal classes and data classes. In a normal class, the default implementation uses reference equality, whereas in a data class, value equality is used.
1class RegularClass(val name: String, val age: Int) 2data class DataClass(val name: String, val age: Int) 3 4fun main() { 5 val regular1 = RegularClass("Eve", 35) 6 val regular2 = RegularClass("Eve", 35) 7 println(regular1 == regular2) // Returns false (reference equality) 8 9 val data1 = DataClass("Eve", 35) 10 val data2 = DataClass("Eve", 35) 11 println(data1 == data2) // Returns true (value equality) 12}
A data class must include at least one parameter in its primary constructor. This contrasts with a simple class, which can have a parameterless constructor.
Data classes can implement interfaces. Here’s an example:
1interface Identifiable { 2 fun getId(): Int 3} 4 5data class Employee(val id: Int, val name: String) : Identifiable { 6 override fun getId() = id 7}
In summary, the choice between a Kotlin class vs. data class depends on your requirements. Use a normal class for complex behaviors or logic-heavy components, and opt for a Kotlin data class for lightweight, immutable objects that store and manage data. By leveraging the power of data classes in Kotlin, you can reduce boilerplate code, improve readability, and streamline your development process.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.