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

Contenu connexe

Tendances

Javascript operators
Javascript operatorsJavascript operators
Javascript operators
Mohit Rana
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatable
leminhvuong
 

Tendances (20)

Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beans
 
Java packages
Java packagesJava packages
Java packages
 
Introduction to kotlin coroutines
Introduction to kotlin coroutinesIntroduction to kotlin coroutines
Introduction to kotlin coroutines
 
Reitit - Clojure/North 2019
Reitit - Clojure/North 2019Reitit - Clojure/North 2019
Reitit - Clojure/North 2019
 
Abstract class
Abstract classAbstract class
Abstract class
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast Receivers
 
Kotlin
KotlinKotlin
Kotlin
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
Autoboxing And Unboxing In Java
Autoboxing And Unboxing In JavaAutoboxing And Unboxing In Java
Autoboxing And Unboxing In Java
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Javascript operators
Javascript operatorsJavascript operators
Javascript operators
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Spring Framework - Validation
Spring Framework - ValidationSpring Framework - Validation
Spring Framework - Validation
 
Stack and heap
Stack and heapStack and heap
Stack and heap
 
Scrollable Updatable
Scrollable UpdatableScrollable Updatable
Scrollable Updatable
 
Idiomatic Kotlin
Idiomatic KotlinIdiomatic Kotlin
Idiomatic Kotlin
 

Similaire à Data classes in kotlin by Naveed

Similaire à Data classes in kotlin by Naveed (20)

Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
 
A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...
A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...
A Generic Neural Network Architecture to Infer Heterogeneous Model Transforma...
 
Object Oriented Programming C#
Object Oriented Programming C#Object Oriented Programming C#
Object Oriented Programming C#
 
Unit-III(Design).pptx
Unit-III(Design).pptxUnit-III(Design).pptx
Unit-III(Design).pptx
 
Programming with Objective-C
Programming with Objective-CProgramming with Objective-C
Programming with Objective-C
 
Object Oriented Programming (Advanced )
Object Oriented Programming   (Advanced )Object Oriented Programming   (Advanced )
Object Oriented Programming (Advanced )
 
OODPunit1.pdf
OODPunit1.pdfOODPunit1.pdf
OODPunit1.pdf
 
OOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and ObjectOOP Unit 2 - Classes and Object
OOP Unit 2 - Classes and Object
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleiner
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Microsoft Entity Framework
Microsoft Entity FrameworkMicrosoft Entity Framework
Microsoft Entity Framework
 
Chap01
Chap01Chap01
Chap01
 
Ef code first
Ef code firstEf code first
Ef code first
 
Go Faster With Native Compilation
Go Faster With Native CompilationGo Faster With Native Compilation
Go Faster With Native Compilation
 
PL SQLDay Machine Learning- Hands on ML.NET.pptx
PL SQLDay Machine Learning- Hands on ML.NET.pptxPL SQLDay Machine Learning- Hands on ML.NET.pptx
PL SQLDay Machine Learning- Hands on ML.NET.pptx
 
CPP15 - Inheritance
CPP15 - InheritanceCPP15 - Inheritance
CPP15 - Inheritance
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 

Dernier

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Dernier (20)

Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
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
  • 3. Introduction • Concise • Null-safe • Interoperable • Easy to write and understand Why should we use Kotlin?
  • 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
  • 6. Motivation // Kotlin Data Class data class User( val name: String, val designation: String? )
  • 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:
  • 9. toString() Imagine we have the next data class:
  • 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.
  • 17. Any Question or Confusion?

Notes de l'éditeur

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