Home »
Kotlin
Data classes in Kotlin
By IncludeHelp Last updated : December 04, 2024
Nowadays classes are the soul of most of the Softwares. Every software contains lots of classes, out of them some of the classes are only used to store data. In this article, we will discuss the easy representation and implementation of those classes knows as data class in kotlin.
Data classes
Data classes are the classes whose main purpose is to store data with some basic functionality. In kotlin we don’t have to bother about writing long code, preparing getter and setters, designing equals()/toString() methods etc. A single keyword in kotlin will do all these jobs for you. These classes are marked with 'data' keyword.
Declaring a Data Class
To declare a data class in Kotlin, use the below syntax:
data class user(val name: String, val password: String,var email :String)
The compiler automatically derives following members from all property declared in the primary constructor.
- Equals()/hashcode() pair,
- toString()
- copy() function
- component() functions corresponding to each properties (getters and setters)
Key Notes
If we explicitly define these methods in the data class body then compiler will not generate these functions and uses existing implementation of them.
Requirements for Data Classes
To ensure that a data class will work in consistent and meaningful manner, we must manage following things,
- Primary constructor must have at least one parameter
- All primary constructor member need to be marked as var or val,
- Data classes cannot be abstract, open or sealed.
- (Before 1.1) Data classes may only implement interfaces. Since 1.1, data classes may extend other classes (Sealed classes).
Java Equivalent Code
In java the above code is written as
public class User {
public String name;
public String password;
public String email;
public String getName() {
return this.name;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object o) {
some code here
}
@Override
public String toString(){
return "User(name="+name+", password= "+password+", email="+email+")";
}
}
Advantage of Using Data Class in Kotlin
But, Kotlin reduces this all code to just one line which will perform all the work as above java program,
data class user(val name: String, val password: String,var email :String)
Instantiation
This is instantiated as same as other classes,
var ob=user("Aman","1234","[email protected]")
Accessing Members
Its members can be accessed as,
println(ob.name) // Aman
println(ob.email) // [email protected]
Copying Objects
Sometime we need to copy an object but altering some properties, keeping the rest same, we can use copy() function.
Here we can do by that,
var ob=user("Aman","1234","[email protected]")
var ob2=ob.copy(email="[email protected]")
This will create an object ob2 with same data except new email.
Built-in Data Classes
The Pair and Triple are built in data classes provided by kotlin for common operations.