C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Kotlin

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() {
    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+")";
    }
}

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","Aman@gmail.com")

Its members can be accessed as,

println(ob.name)     // Aman
println(ob.email)    // Aman@gmail.com

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","Aman@gmail.com")
var ob2=ob.copy(email="amanguatam208@gmail.com")

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.



Advertisements




Liked this article? Do share with your friends :)


COMMENTS