Kotlinlearncs.online LogoJava

    ← Prev


    Next →

    • Objects, Continued : 25

    • Introduction to Objects : 24

    • Compilation and Immutability : 23

    • Practice with Collections : 22

    • Maps and Sets : 21

    • Lists and Type Parameters : 20

    • Imports and Libraries : 19

    • Multidimensional Arrays : 18

    • Practice with Strings : 17

    • null : 16

    • Algorithms and Strings : 15

    • Strings : 14

    • Functions and Algorithms : 13

    • Practice with Functions : 12

    • More About Functions : 11

    • Errors and Debugging : 10

    • Functions : 9

    • Practice with Loops and Algorithms : 8

    • Algorithms I : 7

    • Loops : 6

    • Arrays : 5

    • Compound Conditionals : 4

    • Conditional Expressions and Statements : 3

    • Operations on Variables : 2

    • Variables and Types : 1

    • Hello, world! : 0

    Introduction to Objects

    class Example(var value: Int)
    val example = Example(0)
    example.value = 10

    A new era dawns. We leave the familiar world of basic types and Strings behind and strike out to new horizons. This lesson begins our discussion of Kotlin objects. Objects represent both a significant conceptual step forward, and dramatically improve our ability to work with data.

    So let’s get started!

    What Are Objects?
    What Are Objects?

    Kotlin supports object-oriented programming. What does that mean, exactly?

    Wikipedia defines an object as:

    In the class-based object-oriented programming paradigm, “object” refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures.

    Let’s examine this definition together.

    Object Terminology: Class
    Object Terminology: Class

    A class definines how an entire group of objects behaves. For example, we might say that a person is a class of things where each has a name and an age. Let’s look at our first class definition together:

    class Person(var name: String, var age: Int)

    Object Terminology: Instance
    Object Terminology: Instance

    Defining a class allows us to create instances of that class. Sometimes we use the term object and instance interchangeably, defining an object as an instance of a class.

    If it helps understand the relationship between class and instance, here are some examples of this relationship between real-world things:

    Now, let’s continue the example above using our Person class and create some instances.

    class Person(var name: String, var age: Int)

    Creating Instances
    Creating Instances

    To create a new instance of a class we use the classes name in a way that looks like a method call:

    class Example(var value: Int)
    // Assignment of a new Example instance to the variable e, setting value to 8 initially
    val e = Example(8)

    For now the syntax of instance creation will seem somewhat mysterious. Why does it look like a function call? That will make more sense soon. But for now simply follow this template to create new class instances.

    Accessing Fields Using Dot Notation
    Accessing Fields Using Dot Notation

    Objects allow us to create new Kotlin types. These new types are in some ways just like the types that we’ve already been working with:

    class Example(var value: Int)
    var i = 0 // i is a mutable variable of type int
    val e = Example(12) // e is an immutable variable of type Example

    But Kotlin classes allow us to include any mixture of different types of data by declaring one or more fields:

    // Example has one mutable field named value of type Int
    class Example(var value: Int)
    // I need to set the field when I create Example instances
    val e = Example(8)
    // I can access the field value using dot notation:
    e.value = 10
    // Dog has two fields, one named name of type String and another named age of type Double
    class Dog(var name: String, var age: Double)
    // Again, I can access both Dog fields using dot notation
    val d = Dog("Chuchu", 16.1)
    println("${d.name} is ${d.age} years old")

    Be Patient!
    Be Patient!

    An important note: not everything about objects is going to make sense to you right away! That’s OK. We’re going to keep practicing, and things will become a bit more clear every day.

    Solve: Object with Public Value (Practice)

    Created By: Geoffrey Challen
    / Version: 2021.9.0

    Write a method getExampleValue that, given a passed instance of an Example, returns that Examples value property, which is an Int.

    The Example class looks something like this:

    Solve: Identify a Suspect

    Created By: Miguel Fernandez
    / Version: 2021.8.0

    There was a theft of research hardware on campus last night. Based on eyewitness accounts, they figured out the suspect went through the Siebel Center for Computer Science, the Digital Computing Laboratory, and the Illini Union. Luckily, you have the lists of the people who entered each building from their I-Card ID swipes.

    You've been given three Set<String>s that represent the lists of people that entered each building yesterday. Your job is to create a function called calculateSuspects that when given these parameters returns the list of suspects as a Set<String>. This list should consist of all the names of all people who are included in at least two of the Sets.

    You may find reading the documentation for intersect (intersection) helpful.


    Note that you do not need add any import statements to use sets in Kotlin.

    More Practice

    Need more practice? Head over to the practice page.