Hands-On Object-Oriented Programming with Kotlin

Book description

Learn everything you need to know about object-oriented programming with the latest features of Kotlin 1.3

Key Features

  • A practical guide to understand objects and classes in Kotlin
  • Learn to write asynchronous, non-blocking codes with Kotlin coroutines
  • Explore Encapsulation, Inheritance, Polymorphism, and Abstraction in Kotlin

Book Description

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.

What you will learn

  • Get an overview of the Kotlin programming language
  • Discover Object-oriented programming techniques in Kotlin
  • Understand Object-oriented design patterns
  • Uncover multithreading by Kotlin way
  • Understand about arrays and collections
  • Understand the importance of object-oriented design patterns
  • Understand about exception handling and testing in OOP with Kotlin

Who this book is for

This book is for programmers and developers who wish to learn Object-oriented programming principles and apply them to build robust and scalable applications. Basic knowledge in Kotlin programming is assumed

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Hands-On Object-Oriented Programming with Kotlin
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  5. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  7. Getting Started with Kotlin
    1. Technical requirements
    2. Introduction to Kotlin
    3. Data types in Kotlin
      1. Declaring a variable in Kotlin
        1. The var keyword
        2. The val keyword
        3. Type inference
        4. Type annotation
        5. Number data types
        6. Real data types
        7. Boolean data type
        8. Character data type
        9. Type checking with the is keyword
        10. String variable
    4. Null safety, type casting, Pair, and Triple
      1. Null safety
        1. Safe call operators
        2. The ?. Safe call operator
        3. The ?: Elvis operator
        4. The !! Sure operator
      2. Type casting
        1. Converting from Byte to Float
        2. Converting from Double to Integer
        3. Converting from String to Integer
        4. Smart cast
        5. Unsafe cast
      3. Pair and Triple
        1. How to declare
        2. How to retrieve values
    5. Flow controls
      1. The if statement
      2. The if and else statements
      3. The if statement with a conditional operator
        1. The if statement with the and (&&) operator
        2. The if statement with the or (||) operator
        3. The if statement with the (!) Not operator
        4. if as an expression
        5. The When expression
          1. Combining cases
          2. Ranges with When
          3. When as an expression
    6. Introduction to loops
      1. The while loop
        1. Defining the while loop
      2. The do while loop
      3. The for loop
        1. Defining the for loop
        2. The nested for loop
        3. Break statements
        4. The break statement with labeled for loop
    7. What is a function?
      1. Function declaration
      2. Functions with no parameter and no return type
      3. Function with parameters
      4. Functions with parameters and return types
      5. Function as an expression
      6. Functions with default arguments
      7. Functions with named parameters
      8. Functions and vararg
      9. vararg with other arguments
      10. Package-level functions
        1. How to access a function
    8. Summary
    9. Questions
    10. Further reading
  8. Introduction to Object-Oriented Programming
    1. Technical requirements
    2. Object-oriented programming?
      1. Classes and objects
      2. Attributes and behaviors
    3. Benefits of object-oriented programming
    4. Classes in Kotlin
    5. Properties – first-class citizens
    6. Constructing a class with a constructor
      1. Default constructor
      2. Primary constructor
      3. Primary constructor with the init block
        1. The this keyword
      4. Secondary constructor
        1. Constructor overloading
        2. Constructor with default parameters
        3. Constructor with named parameters
    7. What are data classes?
      1. The toString() function
      2. The equals() function ==
      3. The copy function
      4. The hashCode() function
        1. Object decomposition
    8. Classes and functions
      1. Function overloading
    9. Summary
    10. Questions
    11. Further reading
  9. The Four Pillars of Object-Oriented Programming
    1. Technical requirements
    2. Encapsulation
      1. Information-hiding
      2. Visibility modifier
        1. The private modifier
        2. The protected modifier
      3. Implementation-hiding
    3. What is inheritance?
      1. Using the open keyword
      2. Using the super keyword
      3. Class-initialization hierarchy
    4. Abstraction
      1. Abstract classes
      2. Abstract functions
    5. Interfaces
      1. Multiple interfaces
        1. Resolving conflicts between interfaces
        2. Interfaces and polymorphism
    6. Overriding
      1. What is overriding?
      2. The super keyword
      3. The final keyword
        1. Rules for function-overriding
      4. Property-overriding
    7. Types of inheritance
      1. Single inheritance
      2. Multilevel inheritance
      3. Hierarchical inheritance
      4. Hierarchical multilevel inheritance
      5. The is-a relationship
    8. Polymorphism
      1. How polymorphism works
        1. Dynamic binding
    9. Summary
    10. Questions
    11. Further reading
  10. Classes - Advanced Concepts
    1. Technical requirements
    2. Sealed classes and enumeration
      1. Sealed classes
        1. Sealed classes with the when block
        2. Why do we need sealed classes?
      2. Enum classes
        1. The enum class with a constructor
        2. The enum class and functions
        3. The enum class and interfaces
    3. Objects and companion objects
      1. The object class with inheritance and interfaces
      2. Companion objects
        1. Companion functions and interfaces
    4. Aggregation, composition, and nested classes
      1. Aggregation
      2. Composition
      3. Nested classes
        1. Inner classes
    5. Class properties
    6. Delegate properties
    7. Built-in delegates
    8. Summary
    9. Questions
    10. Further reading
  11. Data Collection, Iterators, and Filters
    1. Technical requirements
    2. Range
      1. Types of ranges
      2. Declaring a range
        1. The rangeTo function
        2. The until function
        3. The downTo function
        4. The step function
        5. The contains function
        6. The elementAt function
        7. Range properties
        8. The coerce functions
          1. The coerceIn function
          2. The coerceAtMost(maxValue) function
          3. The coerceAtLeast(minValue) function
    3. Arrays
      1. Defining arrays
      2. Arrays of a specific type
      3. Index of the array element
        1. The get function
      4. The array and for loop
      5. Array declaration and initialization
        1. The set function
        2. Mutable arrays with immutable elements
        3. Arrays with lambda expressions
        4. Arrays of different elements
        5. The arrayOfNulls function
      6. Copying arrays
    4. Lists in Kotlin
      1. Immutable lists
        1. The listOfNotNull function
        2. List access
      2. Immutable lists and interfaces
        1. Iterable interfaces
        2. Collection interfaces
        3. List interfaces
      3. Mutable lists
        1. The MutableIterable interface
        2. Mutable collection interfaces
        3. Mutable list interfaces
      4. Sets in Kotlin
        1. Immutable sets
        2. Mutable sets
      5. Map
        1. Immutable maps
        2. Mutable maps
      6. Iterators in Kotlin
        1. Iterators
        2. List iterators
        3. Mutable iterators
        4. Mutable list iterators
    5. Filters and collection functions
      1. The filter function
        1. The it keyword
      2. The filterNot function
      3. The filterNotNull function
      4. The filterIsInstance<dataType> function
      5. The slice function
      6. The take function
        1. takeLast
        2. takeWhile
        3. takeLastWhile
      7. The drop function
        1. dropLast
        2. dropWhile
        3. dropWhileLast
      8. The fold function
        1. foldRight
      9. The reduce function
        1. reduceRight
        2. reduceRightIndexed
      10. Some important functions
        1. foreach
        2. forEachIndexed
        3. onEach
        4. max and maxBy
        5. min and minBy
    6. Summary
    7. Questions
    8. Further reading
  12. Object-Oriented Patterns in Kotlin
    1. Technical requirements
    2. What design patterns are and how they help
    3. Types of design patterns
      1. Creational patterns
      2. Structural patterns
      3. Behavioral patterns
    4. Creational patterns
      1. Builder
      2. Factory method
      3. Abstract factory
      4. Singleton
    5. Structural patterns
      1. Adapter
      2. Decorator
      3. Facade
      4. Proxy
    6. Behavioral patterns
      1. Observer
      2. Strategy
      3. Command
      4. State
      5. Chain of responsibility
      6. Visitor
    7. Summary
  13. Coroutines - a Lightweight Thread?
    1. Technical requirements
    2. What are coroutines?
    3. Coroutines and threads
    4. Coroutine examples
      1. Exception handling
      2. Resource releasing
      3. Non-cancelable block
      4. Parallel execution
      5. Lazy execution
      6. Channels
    5. Summary
  14. Interoperability
    1. Technical requirements
    2. Calling Java code in Kotlin
      1. Calling static variables and functions
      2. Calling the Java collection in Kotlin
      3. Reserve words in Kotlin
      4. Java classes in Kotlin
    3. Calling Kotlin code in Java
      1. Calling the Kotlin function
        1. Extension functions
        2. Functions with mutable collections
        3. Functions with immutable collections
        4. Functions with the JVM annotation
      2. Calling the Kotlin class
      3. Calling the Kotlin singleton class
    4. Summary
    5. Questions
    6. Further reading
  15. Regular Expression and Serialization in Kotlin
    1. Technical requirements
    2. Introducing regular expression
    3. Kotlin and regular expression
    4. Introducing serialization
    5. Serialization and Kotlin support
    6. Summary
  16. Exception Handling
    1. Technical requirements
    2. What is exception handling?
    3. Checked and unchecked exceptions
      1. Exception hierarchy
      2. Exceptions in Java
      3. Exceptions in Kotlin
      4. Interoperability
    4. Using the try block as an expression
    5. Writing your own exception
    6. Summary
    7. Questions
    8. Further reading
  17. Testing in Object-Oriented Programming with Kotlin
    1. Technical requirements
    2. Why testing?
    3. Types of testing
      1. Manual testing
      2. Automated testing
        1. Unit testing
    4. The Kotlin test libraries
      1. Installation
        1. Gradle with Groovy
        2. Gradle with the Kotlin script
        3. Maven
    5. Testing examples
      1. Class references
      2. Writing specifications
        1. The given, on, and it blocks
        2. Testing exceptions
        3. The describe and it blocks
    6. Summary
    7. Questions
    8. Further reading
  18. Assessments
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 8
    8. Chapter 10
    9. Chapter 11
  19. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Hands-On Object-Oriented Programming with Kotlin
  • Author(s): Abid Khan, Igor Kucherenko
  • Release date: October 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789617726