Data class is a simple class which is used to hold data/state and contains standard functionality. A data keyword is used to declare a class as a data class. Declaring a data class must contains at least one primary constructor with property argument (val or var).
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
Data classes in kotlin by Naveed
1. Data Classes in Kotlin
Tips and Tricks to use “Data Classes”
Presenter:
Naveed Ahmad
Senior Android Developer
Google Certified Associate Android Developer
https://pk.linkedIn.com/in/naveed99
sr.naveed99@gmail.com
2. Lesson Objectives
• Creating and using Kotlin data classes
• The advantages of data classes over regular classes
• Usage of different data class methods
• Destructuring Declarations
• Limitations of data classes in object-oriented programming
4. Data Classes
• Data classes specialize in holding data
• Compiler automatically generates the following functionality for the data
class:
1. Constructor
2. toString()
3. equals()
4. hashCode()
5. copy()
6. componentN()
5. Requirements for Data Classes
Every Data Class in Kotlin needs to fulfill the following requirements -
The primary constructor must have at least one parameter
All the parameters declared in the primary constructor need to be marked
as val or var
Data classes cannot be abstract, open, sealed or inner
7. Declaring Data Classes
• You declare data classes similar to how you declare
regular classes, except:
• The keyword data must precede the keyword class.
• The primary constructor must not be empty, and it
should contain only val or var properties.
8. Constructing in Data Classes
• Data classes can have two types of
constructors: primary and secondary.
• The primary constructor on a data class can
only declare properties. You can optionally
create a secondary constructor, but it must
delegate to the primary using
the this keyword.
• Here’s an example:
11. Copy()
• To copy a data class but changing one or more of their properties and keeping the rest intact
• Copy function allows us to do that. Just we need to set the property that we want to change
and Kotlin will do the rest
12. Equals()
Equals Method:
You can also use Kotlin’s Structural equality operator == to check for equality. The == operator
internally calls the equals() method
14. ComponentN()
1 2
• Get generated corresponding to the constructor properties in their order of declaration
• Consider an example of our Customer class here we have age and name properties
declared in the primary constructor,
• 2 components methods respectively component1() and component2() will be created
15. Destructuring Declarations
• This is another helpful functionality that we can do with our data classes, we can get as
variables the properties of one data class
16. Limitations
Data classes have a few limitations when compared to regular classes:
• They have little utility other than holding data. They can’t
be open, abstract, sealed or inner classes.
• The compiler forbids manually implementing copy() and componentN() methods.
• Any parent interface or class of a data class must not have a copy() method.
Note: Another cost associated with using data classes is the increased method count in the compiled
code. While not an issue on modern Android versions, it’s something to consider when developing for
older releases.