Data Classes

In every languages, when you want to create object that carry data, you always have to write boilerplate code to
compare, print or even duplicate objects.
To get rid of that, Kotlin introduce the data class concept.

According to the following snippet, in one line, the compiler will create extra members as:

equals() / hashCode()

toString()

copy()

1

2

3

4

5

6

7

8

9

10

11

12

data class Cat(valname:String, valcolor:String)

// { autofold

funmain(args: Array<String>) {

valfelix= Cat("Felix", "White")

valloki= Cat("Loki", "Black/White")

println("felix prints: $felix")

println("felix's hashCode: ${felix.hashCode()}")

println("loki prints: $loki")

println("felix's hashCode: ${loki.hashCode()}")

println("Does felix == loki ? ${felix == loki}")

}

// }

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Tips: Since v1.1, data class can inherit from other classes, but still can't be inherited

Sealed Classes

Kotlin introduced an other type of class, sealeed class. A sealed class, is a class that can be inherit by a
restricted class set. By definition, a sealed class is abstract, that means it can't be directly instantiate.
All derived classes have to be in the same file than the sealed class.