Planning out objects
An object is defined by what attributes it possesses. Let's imagine a glorious bedside alarm clock. You know, that electrical gadget we all love to hate. With minimum effort, we can imagine this alarm clock having a few distinguishing characteristics. I mean, we will likely not mistake this thing as a cow!
There are lots of different types of alarm clocks out there, but somehow we’re still able to recognize the various versions of these irritatingly useful machines as being part of the same category.
Alarm clocks have common attributes. In addition to displaying the current time, they all have a snooze button, a way to set an alarm, an on/off button, and a power source. Despite how physically different they may seem, they all fit into the same kind of master plan or blueprint in our minds. And it’s not so different for computers. As we saw in the video, in programming speak, this kind of blueprint is called a class.
How do classes help us make objects? Well, your computer functions a bit like a manufacturer. Let’s imagine this manufacturer decides they want to build and sell alarm clocks. To make an alarm clock, though, it needs a plan detailing all of the attributes an alarm clock should have. On top of a lovely drawing like the one above, the plan could include something like this:
NAME | ATTRIBUTES |
alarm clock |
|
While this describes the alarm clock, it doesn’t tell us much about what it does. As you can guess, each object has specific behaviors, too. My alarm clock has a snooze bar along the top, and I smack it anytime it starts honking like an angry goose! What action does pressing the snooze bar perform? Well, it goes quiet, and I get a bit more sleep. Another action or behavior is setting how long it should snooze when I press that snooze bar. Thanks to the behaviors it provides me with, there are a couple of buttons on the side that I can scroll through to configure these settings.
So, our plan needs to get extended to include an alarm clock's behavior, which you already know from the video, refers to its methods. Let's add a category and list a few options:
NAME | ATTRIBUTES | METHODS |
alarm clock |
|
|
Notice how each of the methods uses the attributes? That’s one of the cool things about classes - everything you need to use is already encapsulated inside of it!
If only you could get your roommate to stop messing with the time on your alarm! Wouldn’t it be nice to have some kind of time-lock mechanism? Well, in computer programming, you can: we call it access control. It means you can regulate who can use the methods in your class. It’s as if you could freeze out your roommate from touching your alarm clock, no matter how funny they think it is.
From classes to objects
Now that the plan, or class, is all set to go, you can feed it into the assembly line, and bring those alarm clocks to life as objects! A plan is nice, but an object you can really use is better! As we saw in the video, this is called instantiation. Each new object is an instance of a class, or in other words, a specific item that follows the plan. However, this doesn’t mean that each object is going to be exactly the same!
You can use the same basic plan to make different individual objects. Some clocks could be more elaborate and expensive, while others could be basic and cheap. Cool, huh?
How do we do that? Well, when you instantiate an object, you have to get a bit more specific. What kind of display for that alarm clock do you want? What color should it be? What size? In other words, you’ve got to give each of the attributes a specific value.
ATTRIBUTES | EXAMPLE VALUES |
power source | 110V, battery |
alarm time | hh:mm AM/PM, 24hr |
time display | hh:mm AM/PM, 24hr |
alarm volume | 0-11, 0-5 |
snooze interval | 5 minutes, 10 minutes |
color | red, blue |
size | 4inch, 30cm |
With the list of values for our attributes, you can now see the same ones (defined in our plan) can have different values. Each list of values is specific to a single object, or in programming, an instance. This means you’ve got a lot of flexibility. The same plan can provide lots of different individual clocks, just like a house blueprint allows you to make a whole neighborhood of houses!
Try it out for yourself!
Think of a tech object you use every day, and try to sketch out its blueprint (class):
What attributes would you associate with it?
Based on the attributes, what methods would it have?
What access would the methods have? Who could use them?
Now, imagine you've made an instance of that class: an object! What specific values would its attributes have?
Try drawing it out and labeling it. A good doodle can help you visualize how all elements fit together. ✏️
Summary
A plan or blueprint for an object is called a class.
A class defines the attributes (a description), the methods (what it does), and the access level (who can use it) for an object.
To create an object, you have to instantiate it. An instantiated object is called an instance of a class.
Objects from the same class can differ in the details if they have different values for their attributes. They still have to follow the class plan, though!
Encapsulation is the bundling together of attributes, methods, and access control in order to protect an object.