...

/

Classes and Objects (part-II)

Classes and Objects (part-II)

Learn how to use data classes, init blocks, and secondary constructors in our Kotlin program.

The data classes

We can take this one step further by writing the keyword, data, before the compact class:

Press + to interact
data class Person(val firstName: String, val lastName: String)

With this minimal addition, we have created a data class. This is extremely useful for classes whose primary task is that of a data container.

Data classes have a lot of useful features for this purpose. One of these, however, is a real ...