Lesson 3 - Classes and Objects

Download as pdf or txt
Download as pdf or txt
You are on page 1of 56

Lesson 3:

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.

This is technically equivalent to:

Android Development with Kotlin This work is licensed under the Apache 2 license. 10
Initializer block

● Any required initialization code is run in a special init block


● Multiple init blocks are allowed
● init blocks become the body of the primary constructor

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

● Use the constructor keyword to define secondary constructors


● Secondary constructors must call:
○ The primary constructor using this keyword
OR

○ Another secondary constructor that calls the primary constructor


● Secondary constructor body is not required

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

● Define properties in a class using val or var


● Access these properties using
dot . notation with property name
● Set these properties using
dot . notation with property name (only if declared a var)

Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Person class with name property

Access with <property name>


Set with <property name>

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:

● Override get() for a property


● Override set() for a property (if defined as a var)

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

● Classes can also contain functions


● Declare functions as shown in Functions in Lesson 2
○ fun keyword
○ Can have default or required parameters
○ Specify return type (if not Unit)

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

● Kotlin has single-parent class inheritance


● Each class has exactly one parent class, called a superclass
● Each subclass inherits all members of its superclass including
ones that the superclass itself has inherited

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

● Provide a contract all implementing classes must adhere to

● Can contain method signatures and property names

● Can derive from other 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

● Kotlin classes by default are not subclassable

● Use open keyword to allow subclassing

● Properties and functions are redefined with the override


keyword

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)

● Must use override when overriding properties and methods

● Something marked override can be overridden in subclasses


(unless marked final)

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.

● Will the behavior be specific to that type? Consider a class.

● Need to inherit from multiple classes? Consider refactoring code to see


if some behavior can be isolated into an interface.

● Want to leave some properties / methods abstract to be defined by


subclasses? Consider an abstract class.

● 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.

● Appears as if the implementer added it

● Not actually modifying the existing class

● Cannot access private instance variables

Format:

Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Why use extension functions?

● Add functionality to classes that are not

● Add functionality to classes you don’t own

● 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:

Call isOdd() on an Int:

Extension functions are very powerful in Kotlin!

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

● Mark the class with the data keyword

● Generates getters for each property (and setters for vars too)

● Generates toString(), equals(), hashCode(), copy()


methods, and destructuring operators

Format:

Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Data class example
Define the data class:

Use the data class:

Data classes make your code much more concise!

Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Pair and Triple

● Pair and Triple are predefined data classes that store


2 or 3 pieces of data respectively

● Access variables with .first, .second, .third respectively

● Usually named data classes are a better option


(more meaningful names for your use case)

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).

It allows for more readable code

Also used in collections like Map and HashMap

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

● Use this to require instances be one of several constant values


● The constant value is, by default, not visible to you
● Use enum before the class keyword

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

● Sometimes you only want one instance of a class to ever exist

● Use the object keyword instead of the class keyword

● Accessed with NameOfObject.<function or variable>

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

● Lets all instances of a class share a single instance of a set of


variables or functions

● Use companion keyword

● Referenced via ClassName.PropertyOrFunction

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

● Kotlin DOES NOT enforce a single entity (class/interface)


per file convention

● You can and should group related structures in the same file

● Be mindful of file length and clutter

Android Development with Kotlin This work is licensed under the Apache 2 license. 50
Packages

● Provide means for organization

● Identifiers are generally lower case words separated by periods

● Declared in the first non-comment line of code in a file


following the package keyword

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.

● public means visible outside the class. Everything is public by default,


including variables and methods of the class.

● 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

Practice what you’ve learned by


completing the pathway:
Lesson 3: Classes and objects

Android Development with Kotlin This work is licensed under the Apache 2 license. 56

You might also like