Classes

Classes are the foundational type in Kotlin. In Kotlin, a class is a template that provides a state, a behavior, and a type to instances (more on that later).

To define a class, only a name is required:

class VeryBasic

VeryBasic is not very useful, but is still a valid Kotlin syntax.

The VeryBasic class doesn't have any state or behavior; nonetheless, you can declare values of type VeryBasic, as shown in the following code:

fun main(args: Array<String>) {
val basic: VeryBasic = VeryBasic()
}

As you can see, the basic value has a VeryBasic type. To express it differently, basic is an instance of VeryBasic.

In Kotlin, types can be inferred; so, the previous example is equivalent to the following code:

fun main(args: Array<String>) {
val basic = VeryBasic()
}

By being a VeryBasic instance, basic has a copy of the VeryBasic type's state and behavior, namely, none. So sad.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.67.5