...

/

Data Classes and the Any Class

Data Classes and the Any Class

Explore Kotlin’s Any superclass, customize object behaviors, and optimize modeling with data classes.

Use of the Any class

If a class has no explicit parent, its implicit parent is Any, which is a superclass of all the classes in Kotlin. This means that when we expect the Any? type parameter, we accept all possible objects as arguments.

Press + to interact
fun consumeAnything(a: Any?) {
println("Om nom $a")
}
fun main() {
consumeAnything(null) // Om nom null
consumeAnything(123) // Om nom 123
consumeAnything("ABC") // Om nom ABC
}

We can think of Any as an open class with three methods:

  • toString

  • equals

  • hashCode

Tip: Overriding methods defined by Any ...