Kotlin Tutorial 10 — Classes

Nick Apperley
3 min readDec 31, 2016

--

This tutorial will be covering all key aspects of classes, including objects, single instance classes (based on Singleton design pattern), and data classes. Kotlin’s approach to classes is very different from many other programming languages. When defining a class if a body isn’t going to be used then the surrounding curly brackets can be omitted. In IntelliJ create a new Kotlin project in ~/kotlin_projects called kotlin_tutorial10. Create a new Kotlin file in src called main containing the following:

Run the main.kt file. As you can see above an object of MinimalClass has been created and stored in the minimalClass constant. Printing out the object to the console will show its class name, @ character, and unique reference identifier. Every class comes with the following functions (can be overridden):

  • toString (readable string representation for an object like Python’s __str__ method)
  • hashCode (provides a unique identifier for an object like Python’s __repr__ method)
  • equals (used to compare two objects from the same class to see if they are the same)

Kotlin classes like Python classes support properties and constructors but unlike Python Kotlin strongly enforces encapsulation (more on that in a later tutorial), uses functions instead of methods for class behaviour, and has a standard way to define and initialise properties in a single step (val — read only property, var — read/write property). Create a new Kotlin file in src called Order containing the following:

Append the main.kt file with the following:

Append printClass() to the main function in main.kt. Run the main.kt file.

Data Class

Think of them as being in some ways like a Python Named Tuple but with behaviour (can have functions), all properties can be mutable, and can inherit from interfaces (more in a later tutorial). A data class must have a non empty primary constructor that has at least one property as an argument (all constructor parameters must be properties). Create a new Kotlin file in src called Person with the following contents:

In the person constructor there are three read/write properties that are parameters. If val is used instead of var then the properties would be read only, except when they are being initialised in the primary constructor. Append the main.kt file with the following:

Append printDataClass() to the main function in main.kt. Run the main.kt file. Data classes support object de-structuring where an object’s properties are split up into its component parts which are stored in variables or constants. Replace the printDataClass function in main.kt with the following:

Run the main.kt file.

Enum

Basic container holding values (like constants) which isn’t instantiated like a class and is referred to directly. All values in an enum are ordered by when they are defined. Create a new Kotlin file in src called Day with the following contents:

Append the main.kt file with the following:

Append printEnum() to the main function in main.kt. Run the main.kt file.

Object

Rather strange to have object as a keyword in a programming language, but it will make much more sense once you separate the keyword from the OOP concept of object. The use of object on its own in Kotlin can be thought of as a single instance class (Singleton), where an object doesn’t need to be created first before using it. You can find object (using companion object keyword) being used in a class definition that acts as a block for static like functions/properties. Append the main.kt file with the following:

Append printObject() to the main function in main.kt. Create a new Kotlin file in src called objects with the following:

Run the main.kt file.

Conclusion

You have now started to get involved with Kotlin OOP and are ready for the next OOP concept. Next tutorial covers inheritance in Kotlin Tutorial 11 — Inheritance.

TOC

  1. Kotlin Tutorial 1 — Introduction
  2. Kotlin Tutorial 2 — Basic Data Types
  3. Kotlin Tutorial 3 — Basic Operators
  4. Kotlin Tutorial 4 — Handling Input And Output
  5. Kotlin Tutorial 5 — Basic Collections
  6. Kotlin Tutorial 6 — Control Flow
  7. Kotlin Tutorial 7 — Functions
  8. Kotlin Tutorial 8 — Modularity
  9. Kotlin Tutorial 9 — Using An IDE
  10. Kotlin Tutorial 10 — Classes
  11. Kotlin Tutorial 11 — Inheritance
  12. Kotlin Tutorial 12 — Encapsulation And Polymorphism
  13. Kotlin Tutorial 13 — Generics

--

--

No responses yet