Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

Ad

Data Classes in Kotlin
Tips and Tricks to use “Data Classes”
Presenter:
Naveed Ahmad
Senior Android Developer
Google Certi...

Ad

Lesson Objectives
• Creating and using Kotlin data classes
• The advantages of data classes over regular classes
• Usage o...

Ad

Introduction
• Concise
• Null-safe
• Interoperable
• Easy to write and understand
Why should we use Kotlin?

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Próximo SlideShare
Jdbc
Jdbc
Cargando en…3
×

Eche un vistazo a continuación

1 de 17 Anuncio
1 de 17 Anuncio

Data classes in kotlin by Naveed

Descargar para leer sin conexión


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).


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).

Más Contenido Relacionado

Data classes in kotlin by Naveed

  1. 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. 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
  3. 3. Introduction • Concise • Null-safe • Interoperable • Easy to write and understand Why should we use Kotlin?
  4. 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. 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
  6. 6. Motivation // Kotlin Data Class data class User( val name: String, val designation: String? )
  7. 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. 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:
  9. 9. toString() Imagine we have the next data class:
  10. 10. Customize toString method Override toString method
  11. 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. 12. Equals() Equals Method: You can also use Kotlin’s Structural equality operator == to check for equality. The == operator internally calls the equals() method
  13. 13. HashCode()
  14. 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. 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. 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.
  17. 17. Any Question or Confusion?

Notas del editor

  • In Slide Show mode, select the arrows to visit links.

×