Data classes in Kotlin

In this article, we are going to learn about Data classes in Kotlin programming language. Its declaration, example etc.
Submitted by Aman Gautam, on January 04, 2018

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 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.

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.

  1. Equals()/hashcode() pair,
  2. toString()
  3. copy() function
  4. component() functions corresponding to each properties (getters and setters)

Note: if we explicitly define these methods in the data class body then compiler will not generate these functions and uses existing implementation of them.

To ensure that a data class will work in consistent and meaningful manner, we must manage following things,

  1. Primary constructor must have at least one parameter
  2. All primary constructor member need to be marked as var or val,
  3. Data classes cannot be abstract, open or sealed.
  4. (Before 1.1) Data classes may only implement interfaces. Since 1.1, data classes may extend other classes (Sealed classes).

In java the above code is written as

public class User {
 public String name;
 public String password;
 public String email;

    public String getName() {

    public String getPassword() {
        return password;

    public String getEmail() {
        return email;

    public void setName(String name) { = name;

    public void setEmail(String email) { = email;

    public void setPassword(String password) {
        this.password = password;
    public boolean equals(Object o) {

    some code here

    public String toString(){
        return "User(name="+name+", password= "+password+", email="+email+")";

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)

This is instantiated as same as other classes,

var ob=user("Aman","1234","")

Its members can be accessed as,

println(     // Aman
println(    //

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","")
var ob2=ob.copy(email="")

This will create an object ob2 with same data except new email.

Note: Pair and Triple are built in data classes provided by kotlin for common operations.


Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.