In domain-driven design development simple class as bean or POJO or model class in Java. In the POJO class we just create fields and setter/getter
methods to set and retrieve data from is Object.
In Kotlin exactly same as simple POJO class with data
keyword.
data class User(val name: String , val age: Int)
Kotlin compiler provides more benefits using data
keyword. the advantage of use data class instead of regular class is that Kotlin gives us an immense amount of self-generated code.
Let’s see one by one….
- The getters and setters for all properties which are in its primary constructor.
2. equals()
and hashcode()
methods.
3. toString()
method.
4. copy functions with default argument
How to create Data class in Kotlin
- You need to append the class with the keyword
data
data class User(val name:String)
- The primary constructor needs to have at least one parameter.
data class User() this is invalid declaration of data class
- Each parameter of the primary constructor must have val/var assigned.
data class User(name:String) WRONG
data class User(val name:String) CORRECT
- Modifiers
abstract
,open
,sealed
orinner
are incompatible withdata
- Data class can extend other classes, also can implement interfaces.
Model classes in java v/s data class in Kotlin
In Java
public class User {
private String name;
private int age;
public User(String name, int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return age == user.age &&
name.equals(user.name);
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result;
return result;
}
@NonNull
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age='" + age +
'}';
}
}
In Kotlin
data class User(val name:String, var age:Int)
Initialization
Java
User user = new User("User Name",65);
Kotlin
var user: User = User("User Name",65)
Usage
Java
user.setName("User Name"); user.getName();
user.setAge(65); user.getAge();
Kotlin
user.name & user.age
Properties declared in the class body
data class User(val email: String) {
var age: Int = 0
}
Only the property email
will be used inside the toString()
, equals()
, hashCode()
, and copy()
implementations, and there will only be one component function component1()
. While two User
objects can have different ages, they will be treated as equal.
Copying
The copy()
function to copy an object, allowing you to alter some of its properties while keeping the rest unchanged. The implementation of this function for the User
class above would be as follows:
fun copy(name: String = this.name, age: Int = this.age) = User(name, age)
val user = User(name = "Dinesh", age = 33)
val olderUser = user.copy(age = 34)