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)

What this book covers

Chapter 1, Getting Started with Kotlin, provides a brief overview of the Kotlin programming language. This chapter also covers data types and how null safety and type casting works. It also provides an introduction to operators and flow controls. At the end of this chapter, we will look at loops and functions.

Chapter 2, Introduction to Object-Oriented Programming, discusses what object-oriented programming is and why it is different and better than procedural programming. This chapter explains classes and data classes in Kotlin, and how Kotlin makes them unique. It also discusses in detail constructors, types of constructors, and properties, and what is meant by properties as first class citizens.

Chapter 3, The Four Pillars of Object-Oriented Programming, provides a thorough description of four pillars of object-oriented programming. It also explains what encapsulation is and how inheritance works. We also look into topics such as why choose polymorphism and what the benefits of abstraction are. Finally, we will learn how to declare a parent class and how to utilize resources in child class. In addition to defining derived classes, this chapter discusses how variables and functions can be redefined in child class, explicit type casting, and implicit type conversion within class hierarchies.

Chapter 4, Classes – Advanced Concepts, describes the key concepts of programming that are provided exclusively by Kotlin. This chapter discusses the concepts of sealed classes, object and companion objects, and why they are different from traditional classes. It also explains properties and delegates in classes.

Chapter 5, Data Collection, Iterators, and Filters, describes the handling of data collection by using different data structures provided in Kotlin. We also learn how to define and use arrays and how to utilize different templates for the efficient management of object collections such as lists, sets, collections, and maps. Besides discussing different data collection techniques, this chapter explains the power of interfaces used in collection classes. You will learn what an iterator is, how iterators are used with different collections, and the issues you may encounter while using it. This chapter also explains what filters are, how filters work, and how to ignore irrelevant data. At the end, this chapter elaborates on how to write your own filters.

Chapter 6, Object-Oriented Patterns in Kotlin, deals with the advanced concepts of object-oriented programming referred to as design patterns. Design patterns are a general solution to a known problem in software design. Design patterns can speed up development processes and reduce the risk of design errors that may occur at a later stage. Different design patterns are used for different problems; this chapter discusses software design challenges and design patterns. Each design pattern is subdivided into different categories that will be discussed in further detail.

Chapter 7, Coroutine – a Lightweight Thread?, describes how Kotlin has introduced a new library called coroutine. Coroutines can be considered a lightweight thread that do not push CPU to its limits, even when thousands of requests are in progress. This chapter explains what a coroutine is, and why it is superior to traditional threads in Java.

Chapter 8, Interoperability, discusses the fact that Kotlin is a superset of Java that is designed with Java interoperability in mind. Interoperability means you can call Java functions in Kotlin, and vice versa, and you can have both Java and Kotlin files in the same application. This chapter discusses in detail the technology behind interoperability and how to write cross-platform code. Most importantly, it also explains how to handle nullable calls that may appear from Java.

Chapter 9, Regular Expression and Serialization in Kotlin, describes how regular expressions, also known as regex, are a combination of different characters that helps to find and locate the necessary information from a huge amount of text. This technique is also called pattern matching, where users provide a string (pattern) and get a list of exact or similar matches. All advanced text editors use regular expressions to find the variable in your code. In this chapter, you will see that Kotlin has provided complete support for regular expressions and how they support writing bug-free patterns. This chapter also discusses serialization and how Kotlin works with text formats such as JSON.

Chapter 10, Exception Handling, discusses exceptions and exception handling in detail. We also learn about what the different keywords are try, catch, throw finally, and more, the different types of exceptions, and how a user can create user-defined exceptions and handle them. This chapter also explains how to prepare test environments to be able to write some tests.

Chapter 11, Testing in Object-Oriented Programming with Kotlin, provides an insight into the importance of testing, why testing is required, and the repercussions if code is released without testing. This chapter also explains the testing techniques, the dedicated Kotlin library for testing, and how Kotlin can help to write clean and readable test cases.