Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - DATA CLASS


A Data Class is just like any other Kotlin class which comes with some added functionality.


A Data Class already has built in functions that makes your life a lot easier.


They are :

  1. copy() Function

  2. equals() Function

  3. hashCode() Function

  4. toString() Function

Now, before we understand the usage of each Function, let us see the conditions to create a Data Class.


Conditions to create Data Class

  1. The primary constructor must have at least one parameter.

  2. The parameters of the primary constructor must be either val or var.

  3. The Data classes must not be open, abstract, inner or sealed.

  4. The Data classes can only implement interfaces. However, after Kotlin version 1.1, the Data classes can extend classes and implement interfaces.

Now, let us see, how can we define a Data Class.


Example :



data class Human(var name: String, var food: String, var language: String)

fun main() {
    var human = Human("Rakhi", "Rice", "Hindi")
    println(human.name+" eats "+human.food+" and speaks "+human.language)
}


Output :



  Rakhi eats Rice and speaks Hindi

So, in the above code, we have defined a data class,


data class Human(var name: String, var food: String, var language: String)

And yes, it is that simple.


It begins with a data keyword and there is no body in it.


Now, let us see the use of the functions it supports by default.


copy() Function


Using the copy() Function, we can make an exact copy of an object.


Example :



data class Human(var name: String, var food: String, var language: String)

fun main() {

    var human1 = Human("Rakhi", "Rice", "Hindi")
    println("Before Copying")
    println(human1.name+" eats "+human1.food+" and speaks "+human1.language)

    var human2 = human1.copy()
    println("After Copying")
    println(human2.name+" eats "+human2.food+" and speaks "+human2.language)
}


Output :



  Before Copying
  Rakhi eats Rice and speaks Hindi
  After Copying
  Rakhi eats Rice and speaks Hindi

So, in the above code, we have defined an object human1,


var human1 = Human("Rakhi", "Rice", "Hindi")

And made a copy of it using the copy() Function(Already defined in the data class),


var human2 = human1.copy()

And initialised to human2 object.


hashCode() and equals() Function


In order to check if two objects has equal values, it is essential to override/redefine the hashCode() and equals() Function.


But thanks to the data class. The data class has the hashCode() and equals() Function defined in it.


Example :



data class Human(var name: String, var food: String, var language: String)

fun main() {

    var human1 = Human("Rakhi", "Rice", "Hindi")

    var human2 = Human("Rakhi", "Rice", "Hindi")
    
    if(human1.equals(human2))
    	println("Two objects are equal")
    else
    	println("Two objects are not equal")
}


Output :



  Two objects are equal

So, in the above code, we have defined two objects,


var human1 = Human("Rakhi", "Rice", "Hindi")
var human2 = Human("Rakhi", "Rice", "Hindi")

With equal values.


And when we use the equals() method to check if two objects are equal or not.


if(human1.equals(human2))
	println("Two objects are equal")
else
	println("Two objects are not equal")

Now, since two objects are equal, we get the output,


Two objects are equal

So, you might be thinking, if two objects are equal, the equals() method should return that the objects are equal.


Well! It's not the case.


Let us try the same example using a class(Not data class).


Example :



class Human(var name: String, var food: String, var language: String){
    
}

fun main() {

    var human1 = Human("Rakhi", "Rice", "Hindi")

    var human2 = Human("Rakhi", "Rice", "Hindi")

    if(human1.equals(human2))
        println("Two objects are equal")
    else
        println("Two objects are not equal")
}


Output :



  Two objects are not equal

Well, in this case we got the output as,


Two objects are not equal

That is because we have declared a simple class,


class Human(var name: String, var food: String, var language: String){
}

Which doesn't have a hashCode() and equals() method defined.


Note : If hashCode() and equals() function is bothering you, take a look at it.

toString() Function


The toString() Function displays the contents of an object in a structured manner.


Example :



data class Human(var name: String, var food: String, var language: String)

fun main() {

    var human1 = Human("Rakhi", "Rice", "Hindi")
    println(human1.toString())
}


Output :



  Human(name=Rakhi, food=Rice, language=Hindi)

So, if you look at the output,


Human(name=Rakhi, food=Rice, language=Hindi)

You can see that the toString() Function,


println(human1.toString())

Displays the contents of an object in a structured way.