Basic Classes

Because Kotlin lets you just write and invoke functions, you can accomplish a fair bit without actually creating your own classes. But, eventually, you will want to create classes, to model data and related behavior.

Basic Classes

Classes in Kotlin are very similar to their Java and Ruby counterparts: they primarily contain properties and functions that operate on those properties.

A class does not have to contain any of that — this is a valid Kotlin class definition:

class Foo {
  
}

So is this:

class Foo

Having a class with no body seems odd. However, in Kotlin, you will occasionally see that sort of thing, particularly with data classes and sealed classes. However, most ordinary classes will have properties and functions, as we will see below.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.