Book Image

Hands-On Object-Oriented Programming with Kotlin

By : Abid Khan, Igor Kucherenko
Book Image

Hands-On Object-Oriented Programming with Kotlin

By: Abid Khan, Igor Kucherenko

Overview of this book

Kotlin is an object-oriented programming language. The book is based on the latest version of Kotlin. The book provides you with a thorough understanding of programming concepts, object-oriented programming techniques, and design patterns. It includes numerous examples, explanation of concepts and keynotes. Where possible, examples and programming exercises are included. The main purpose of the book is to provide a comprehensive coverage of Kotlin features such as classes, data classes, and inheritance. It also provides a good understanding of design pattern and how Kotlin syntax works with object-oriented techniques. You will also gain familiarity with syntax in this book by writing labeled for loop and when as an expression. An introduction to the advanced concepts such as sealed classes and package level functions and coroutines is provided and we will also learn how these concepts can make the software development easy. Supported libraries for serialization, regular expression and testing are also covered in this book. By the end of the book, you would have learnt building robust and maintainable software with object oriented design patterns in Kotlin.
Table of Contents (14 chapters)

Built-in delegates

Aside from user-defined delegates, the Kotlin library also provides some useful built-in delegates that can be used in our applications. One of these delegates is the observable delegate, which triggers when a value is assigned to the property.

The observable takes the following parameters:

  • The first parameter is the initial value of the property
  • The second parameter is a lambda expression that takes the property type, the old value, the new value, and the body of the lambda expression

The following example shows the observable delegate with the age and name properties of the Person class. This delegate observes the properties and is triggered every time the value of the property changes:

class Person {
var age : Int by Delegates.observable(0) { property, oldValue, newValue ->
println("oldValue $oldValue newValue $newValue")
...