2.2. Classes

When we want to interact with and manipulate objects using a computer, we have to teach our computers about the objects in question first. Remember, computers are dumb. They must be given a very detailed, step-by-step guide (AKA a program) in order to do anything.

The name of the step-by-step guide we give our computers to think about objects is called a class. You can think of a class as the recipe your computer must follow in order to create and interact with objects of your choosing. We will learn more about how to specifically write these recipes in our next unit. For now, I want you to really focus on understanding how classes and objects are related and how they are different.

Here’s the big idea:

../../_images/GeneralIdea.png

Fig. 2.4 Your computer uses a class to make objects.

I will say it again! A class is the blueprint your computer needs to make objects. Notice the bold letter s? I added that to highlight the fact that a class can be used to make more than one object of a single type. How so? Meet a cat object:

../../_images/Cat.png

Fig. 2.5 This is a cat object.

It’s possible that this cat object was created from a Cat class. In other words, someone wrote a recipe for making all kinds of cats; purple cats, big cats, sleepy cats, you name it, the Cat class can create a diverse group of cats!

../../_images/Cats.png

Fig. 2.6 Cat objects galore!

It’s also possible that our cat object was created from something a little more generic, like a Mammal Class. Our computers can follow this recipe to make all kinds of mammals. For example, maybe a cat object AND a fox object were created from the same Mammal class.

../../_images/foxcat.png

Fig. 2.7 Fox and cat objects.

Fancy, right? As long as a group of objects have some things in common, then there is no reason why a single class can’t be used to make them all.