Lesson 3 - Classes and Objects
Lesson 3 - Classes and Objects
Lesson 3 - Classes and Objects
Classes and
objects
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 3: Classes and objects
○ Classes
○ Inheritance
○ Extension functions
○ Special classes
○ Organizing your code
○ Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Classes
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Class
Object
● Classes are blueprints for objects
instances
● Classes define methods that operate
on their object instances
Class
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Class versus object instance
House Class Object Instances
Data
● House color (String)
● Number of windows (Int)
● Is for sale (Boolean)
Behavior
● updateColor()
● putOnSale() FOR SALE
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Define and use a class
Class Definition Create New Object Instance
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Constructors
When a constructor is defined in the class header, it can contain:
● No parameters
class A
● Parameters
○ Not marked with var or val → copy exists only within scope of the
constructor
class B(x: Int)
○ Marked var or val → copy exists in all instances of the class
class C(val y: Int)
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
Constructor examples
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
Default parameters
Class instances can have default values.
● Use default values to reduce the number of constructors needed
● Default parameters can be mixed with required parameters
● More concise (don’t need to have multiple constructor versions)
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
Primary constructor
Declare the primary constructor within the class header.
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Initializer block
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
Initializer block example
Use the init keyword:
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
Multiple constructors
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Multiple constructors example
Android Development with Kotlin This work is licensed under the Apache 2 license. 14
Properties
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Person class with name property
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Custom getters and setters
If you don’t want the default get/set behavior:
Format:
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Custom getter
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Custom setter
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Member functions
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Inheritance
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
Inheritance
If you don't want to be limited by only inheriting a single class, you can define an
interface since you can implement as many of those as you want.
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
Interfaces
Format:
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
Interface example
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
Extending classes
To extend a class:
● Create a new class that uses an existing class as its core
(subclass)
● Add functionality to a class without creating a new one
(extension functions)
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Creating a new class
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Classes are final by default
Declare a class
Try to subclass
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Use keyword
Use open to declare a class so that it can be subclassed.
Declare a class
Subclass from C
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Overriding
● Must use open for properties and methods that can be overridden
(otherwise you get compiler error)
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
Abstract classes
● Class is marked as abstract
● Cannot be instantiated, must be subclassed
● Similar to an interface with the added the ability to store state
● Properties and functions marked with abstract must be
overridden
● Can include non-abstract properties and functions
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
Example abstract classes
abstract class Food {
abstract val kcal : Int
abstract val name : String
fun consume() = println("I'm eating ${name}")
}
class Pizza() : Food() {
override val kcal = 600
override val name = "Pizza"
}
fun main() {
Pizza().consume() // "I'm eating Pizza"
}
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
When to use each
● Defining a broad spectrum of behavior or types? Consider an interface.
● You can extend only one class, but implement one or more interfaces.
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Extension functions
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Extension functions
Add functions to an existing class that you cannot modify directly.
Format:
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Why use extension functions?
● Separate out core API from helper methods for classes you
own
Define extension functions in an easily discoverable place such as in the same file
as the class, or a well-named function.
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Extension function example
Add isOdd() to Int class:
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Special classes
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
Data class
● Special class that exists just to store a set of data
● Generates getters for each property (and setters for vars too)
Format:
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Data class example
Define the data class:
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Pair and Triple
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Pair and Triple examples
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Pair
Pair's special to variant lets you omit parentheses and periods (infix function).
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Enum class
User-defined data type for a set of named values
Format: …
Referenced via
Android Development with Kotlin This work is licensed under the Apache 2 license. 43
Enum class example
Define an enum with red, green, and blue colors.
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Object/singleton
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Object/singleton example
Android Development with Kotlin This work is licensed under the Apache 2 license. 46
Companion objects
Android Development with Kotlin This work is licensed under the Apache 2 license. 47
Companion object example
Android Development with Kotlin This work is licensed under the Apache 2 license. 48
Organizing your code
Android Development with Kotlin This work is licensed under the Apache 2 license. 49
Single file, multiple entities
● You can and should group related structures in the same file
Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Packages
package org.example.game
Android Development with Kotlin This work is licensed under the Apache 2 license. 51
Example class hierarchy
org.example.vehicle
Vehicle
org.example.vehicle.moped org.example.vehicle.car
Moped Car
Moped50cc Sedan
Moped100cc Hatchback
Android Development with Kotlin This work is licensed under the Apache 2 license. 52
Visibility modifiers
Use visibility modifiers to limit what information you expose.
● private means it will only be visible in that class (or source file if you are
working with functions).
● protected is the same as private, but it will also be visible to any
subclasses.
Android Development with Kotlin This work is licensed under the Apache 2 license. 53
Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 54
Summary
In Lesson 3, you learned about:
● Classes, constructors, and getters and setters
● Inheritance, interfaces, and how to extend classes
● Extension functions
● Special classes: data classes, enums, object/singletons, companion
objects
● Packages
● Visibility modifiers
Android Development with Kotlin This work is licensed under the Apache 2 license. 55
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 56