Language Elements

Data Types

Kotlin Nullable Types

Exctension Functions

Lambda Functions

Object Oriented Kotlin

Data Classes

Collections

Kotlin Example Codes

Kotlin Interview Questions

In comparison to Java, Kotlin provides a simple way to declare data classes, with much less boiler plate code, without any need of adding gettter and setter (generated by the compiler). The key word data is used to create such class. From this key word, compiler identifies a data class, and then create functions to manage it. To define a data class, one needs list of property names, types and optional initial values.

Kotlin Data class requirements:

  • The primary constructor must have at least one parameter, marked as val or var.

  • The class cannot be marked as open, abstract, sealed or inner.

  • The class may extend other classes or implement interfaces.


  • Example of Kotlin Data Class
    
    data class Employer(val companyName: String, val marketValueInBillions: Int)
    
    
    fun main() {
        val s1 = Employer("abcd", 4)
        println("companyName= ${s1.companyName}")
        println("marketValueInBillions = ${s1.marketValueInBillions}")
    }
    
    



    Copyright © by Zafar Yasin. All rights reserved.