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 :
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.
This function returns a string of all the parameters defined in the data class .
man(roll=1, name=man, height=50)
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.
Here height is not used by the toString() function .
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) .
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
835510190 -938448478 835510190 hash1 == hash 2 false hash2 == hash 3 false hash1 == hash 3 true
man1 and man2 have same object contents, so they are equal, thus they have same hash code values.
- Kotlin Sealed Classes
- Enum Classes in Kotlin
- Kotlin Inline classes
- Kotlin Data Types
- Private Classes in Ruby
- Understanding Classes and Objects in Java
- C# | Abstract Classes
- CSS | Pseudo-classes
- Abstract Classes in Python
- jQuery | Get and Set CSS Classes
- jQuery | multiple classes Selector
- Use of :even and :odd pseudo-classes with list items in CSS
- Generic Classes in Scala
- When to use static vs instantiated classes in PHP?
- Type Aliases vs Inline Classes
- Perl | Classes in OOP
- .pull-left and .pull-right classes in Bootstrap 4
- ES6 | Classes
- How to specify the order of classes in CSS ?
- How to apply two CSS classes to a single element ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.