Introduction to Objects

Created By: Geoffrey Challen
/ Updated: 2022-09-20

A new era begins in CS 124. 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?

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.

Go through the definition of an object. Uniting variables and methods, data structures and algorithms.

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:

Discuss the Person class definition.

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:

  • Dog (class) v. my dog Chuchu (instance)
  • Computer (class) v. my laptop (instance)
  • Building (class) v. the Siebel Center for Computer Science (instance)
  • CS 124 student (class) v. you (instance)

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

Create some instances of Person and show how each has its own properties. Discuss the instance syntax where we pass initial values.

Creating Instances

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

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

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:

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

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.

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Show how to complete the homework problem above. Feel free to cover multiple approaches!

More Practice

Need more practice? Head over to the practice page.