DEV Community

Cover image for Quick introduction to Kotlin classes
Tristan Elliott
Tristan Elliott

Posted on

Quick introduction to Kotlin classes

Introduction

  • This series is not going to be in any particular order, so feel free to read whatever blog post you want. Anytime I find something that I think could use a blog post, I will write one and put it here

Getting started

  • By the end of this tutorial I want both you and I to have a better understanding of the code below:
data class Calf(

    val tagNumber: String,
    val CCIANumber: String,
    val sex:String,
    val details:String,
    val date: Date,
    val id: Long =0
) {

}
Enter fullscreen mode Exit fullscreen mode
  • This tutorial will not be an exhaustive one, however, it should give you a solid understanding of the incredible power of Kotlin's class system. If you would like to know more about classes in Kotlin, I would recommend the documentation,HERE, and the book,Kotlin in action by Dmitry Jemerov and Svetlana Isakova.

Classes

  • Classes in Kotlin are declared with the typical class keyword and they contain three parts:

1) Class name : What we end up calling the file, in the code block above the name is Calf

2) Class header : contains the type parameters(more on parameters later), the primary constructor and modifiers.

3) Class body : containing everything inside the { } braces

  • fun fact, both the class body and the class header are optional if they are both empty

Constructor

  • A class in Kotlin can have a primary constructor and one or more secondary constructor(we won't be talking about primary constructors here),for more information about secondary constructors go HERE for the documentation.
  • In our code block above the primary constructor is:
(

    val tagNumber: String,
    val CCIANumber: String,
    val sex:String,
    val details:String,
    val date: Date,
    val id: Long =0
) 

Enter fullscreen mode Exit fullscreen mode
  • It comes after the class name. As you can see the primary constructor can not contain any initialization code. If initialization code is needed it can be placed inside of an Initializer block.

  • Kotlin has a very precise syntax for declaring PROPERTIES and initializing them from the primary constructor. You may of noticed that I put quite the emphasis on property. That is because before we can move any farther, we need to have a solid understanding of what a property actually is.

Property : a combination of a field and its accessor method.

  • So when we talk about properties, we mean both the field that holds the data and the methods used to set or access the data.
  • Notice that in a primary constructor we have properties defined with val,val tagNumber: String. The val keyword means a value is set only once and we can not change it. In terms of the accessor methods, a property declared with val only has a getter method.

data class

  • The Kotlin compiler can generate useful methods to avoid a verbose code base. Declaring a class as a data class instructs the compiler to generate several methods. The most common and well know being, equals() and hashcode(). In Kotlin the default way to check quality is the use the == operator. Under the hood the compiler will call the equals() method(avoiding the common Java bug of using == instead of equals()) and give us what we call structural equality. If you still want Referential equality(two references point to the same object) use the === operator. Both the equals() and hashcode() take into account all the properties declared in the primary constructor. The generated equals() method checks and validates all values of the properties are equal. The hashcode() method returns a value that depends on the hashcode(unique number to identify the object) of all the properties.
  • I should also point out that if the properties are not declared in the primary constructor, they will not be part of the equals() and hashcode() methods.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)