Kotlin Data Classes
We often create classes to hold some data in it. In such classes, some standard functions are often derivable from the data. In Kotlin, this type of class is known as data class and is marked as data.
Example of a data :
data class Student(val name: String, val roll_no: Int)
The compiler automatically derives the following functions :
- equals()
- hashCode()
- toString()
- copy()
Rules to create Data classes –
Data classes have to fulfill the following requirements to ensure the consistency:
- The primary constructor needs to have at least one parameter.
- All primary constructor parameters need to be marked as val or var.
- Data classes cannot be abstract, open, sealed or inner.
- Data classes may only implement interfaces.
toString()
This function returns a string of all the parameters defined in the data class .
Example:
fun main(args: Array<String>) { //declaring a data class data class man(val roll: Int,val name: String,val height:Int) //declaring a variable of the above data class //and initializing values to all parameters val man1=man( 1 , "man" , 50 ) //printing all the details of the data class println(man1.toString()); } |
Output:
man(roll=1, name=man, height=50)
Note:
The compiler only uses the properties defined inside the primary constructor for the automatically generated functions.
It excludes the properties that are declared in the class body.
Example:
fun main(args: Array<String>) { //declaring a data class data class man(val name: String, val age: Int) { //property declared in class body var height: Int = 0 ; } val man1 = man( "manish" , 18 ) //copying details of man1 with change in name of man val man2 = man1.copy(name= "rahul" ) //copying all details of man1 to man3 val man3 = man1.copy(); //declaring heights of individual men man1.height= 100 man2.height= 90 man3.height= 110 //man1 & man3 have different class body values, //but same parameter values //printing info all 3 men println( "${man1} has ${man1.height} cm height" ) println( "${man2} has ${man2.height} cm height" ) println( "${man3} has ${man3.height} cm height" ) } |
Output:
man(name=manish) 70
Here height is not used by the toString() function .
copy()
Sometimes we need to copy an object, with some change in some of its properties keeping all others unchanged.
In this case copy() function is used.
Properties of copy()
- It copies all arguments or members defined in primary constructor
- Two objects can have same primary parameter values and different class body values if defined
Declaration of copy()
fun copy(name: String = this.x, age: Int = this.y) = user(x, y)
where user is a data class : user(String, Int) .
Example
fun main(args: Array<String>) { //declaring a data class data class man(val name: String, val age: Int) val man1 = man( "manish" , 18 ) val man2 = man1.copy(name= "rahul" ) val man3 = man1.copy(); val hash1=man1.hashCode(); val hash2=man2.hashCode(); val hash3=man3.hashCode(); println(hash1) println(hash2) println(hash3) //checking equality of these hash codes println( "hash1 == hash 2 ${hash1.equals(hash2)}" ) println( "hash2 == hash 3 ${hash2.equals(hash3)}" ) println( "hash1 == hash 3 ${hash1.equals(hash3)}" ) } |
Output:
man(name=manish, age=18) has 100 cm height man(name=rahul, age=18) has 90 cm height man(name=manish, age=18) has 110 cm height
hashCode() and equals()
- hashCode() function returns a hash code value for the object.
- equals() method return true if two objects have same contents and it works similar to “==”, but works different for Float and Double values.
Declaration of hashCode() :
open fun hashCode(): Int
Properties of hashCode()
- Two hash codes declared two times on same object will be equal.
- If two objects are equal according to equals() method, then the hash codes
returned will also be same
<div id= "highlighter_36649" class = "syntaxhighlighter nogutter " ><table border= "0" cellpadding= "0" cellspacing= "0" ><tbody><tr><td class = "code" ><div class = "container" ><div class = "line number1 index0 alt2" ><code class = "plain" >fun main(args: Array<String>) </code></div><div class = "line number2 index1 alt1" ><code class = "plain" >{ </code></div><div class = "line number3 index2 alt2" ><code class = "undefined spaces" > </code><code class = "comments" > //declaring a data class </code></div><div class="line number4 index3 alt1"><code class="undefined spaces"> </code><code class="plain">data </code><code class="keyword">class</code> <code class="plain">man(val name: String, val age: Int) </code></div><div class="line number5 index4 alt2"><code class="undefined spaces"> </code> </div><div class="line number6 index5 alt1"><code class="undefined spaces"> </code><code class="plain">val man1 = man(</code><code class="string">"manish"</code><code class="plain">,</code><code class="value">18</code><code class="plain">) </code></div><div class="line number7 index6 alt2"><code class="undefined spaces"> </code><code class="plain">val man2 = man1.copy(name=</code><code class="string">"rahul"</code><code class="plain">) </code></div><div class="line number8 index7 alt1"><code class="undefined spaces"> </code><code class="plain">val man3 = man1.copy(); </code></div><div class="line number9 index8 alt2"><code class="undefined spaces"> </code> </div><div class="line number10 index9 alt1"><code class="undefined spaces"> </code><code class="plain">val hash1=man1.hashCode(); </code></div><div class="line number11 index10 alt2"><code class="undefined spaces"> </code><code class="plain">val hash2=man2.hashCode(); </code></div><div class="line number12 index11 alt1"><code class="undefined spaces"> </code><code class="plain">val hash3=man3.hashCode(); </code></div><div class="line number13 index12 alt2"><code class="undefined spaces"> </code> </div><div class="line number14 index13 alt1"><code class="undefined spaces"> </code><code class="plain">println(hash1) </code></div><div class="line number15 index14 alt2"><code class="undefined spaces"> </code><code class="plain">println(hash2) </code></div><div class="line number16 index15 alt1"><code class="undefined spaces"> </code><code class="plain">println(hash3) </code></div><div class="line number17 index16 alt2"><code class="undefined spaces"> </code> </div><div class="line number18 index17 alt1"><code class="undefined spaces"> </code><code class="comments">//checking equality of these hash codes </code></div><div class="line number19 index18 alt2"><code class="undefined spaces"> </code><code class="plain">println(</code><code class="string">"hash1 == hash 2 ${hash1.equals(hash2)}"</code><code class="plain">) </code></div><div class="line number20 index19 alt1"><code class="undefined spaces"> </code><code class="plain">println(</code><code class="string">"hash2 == hash 3 ${hash2.equals(hash3)}"</code><code class="plain">) </code></div><div class="line number21 index20 alt2"><code class="undefined spaces"> </code><code class="plain">println(</code><code class="string">"hash1 == hash 3 ${hash1.equals(hash3)}"</code><code class="plain">) </code></div><div class="line number22 index21 alt1"><code class="undefined spaces"> </code> </div><div class="line number23 index22 alt2"><code class="plain">} </code></div></div></td></tr></tbody></table></div> |
Output:
835510190 -938448478 835510190 hash1 == hash 2 false hash2 == hash 3 false hash1 == hash 3 true
Explanation:
man1 and man2 have same object contents, so they are equal, thus they have same hash code values.