The import keyword

Often, classes and types need to make use of other classes and types existing outside the package in which they are declared. This can be done by importing package resources. If two classes belong in the same package, no import is necessary:

package animalsdata class Buffalo(val mass: Int, val maxSpeed: Int, var isDead: Boolean = false)

In the following code snippet, the Buffalo class does not need to be imported into the program because it exists in the same package (animals) as the Lion class:

package animalsclass Lion(val mass: Int, val maxSpeed: Int) {  fun kill(animal: Buffalo) { // Buffalo type used with our import    if (!animal.isDead) {      println("Lion attacking animal.")      animal.isDead = true      println("Lion kill successful.") ...

Get Kotlin Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.