“Object Oriented Programming” may sound like a super advanced term, and a lot of people use it like it’s something you won’t be able to understand until you have years and years of programming experience behind you.
But that’s not true. You can learn it right now. You can even learn it without knowing any programming. Let’s look at the non-programming concept first, and then I’ll show you a few examples in different programming languages.
First a little history if you’re interested (otherwise skip to the next section!)
The term Object-Oriented Programming came out in the early 80s and was at the time the cool, new way of programming. There was a lot of resistance to it, because to older programmers (most of whom are probably long-since retired today!) it seemed completely crazy. It seemed like a bizarre, practically unusable way of programming. “Surely this would only work for a few situations,” people would say.
But it stuck. Soon new programming languages came out that supported it, and existing languages were enhanced to support it. (C++ is one of the biggest examples; its creator, a guy named Bjarne Stroustrup took what was one of the currently most popular languages, C, and added object oriented features to it. In the C language, to add 1 to a variable, you type the variable name followed by two plus signs. So Stroustrup decided to do the same thing with the language name, C, and he came up with the clever name C++.)
Eventually people realized how powerful this new way of programming was, and slowly but surely it gained widespread adoption, to the point that almost all languages today support object-oriented programming.
In fact, in many languages, it’s impossible not to use object-oriented programming.
Object-oriented programming is so common and so popular, that some of us think the name needs to be dropped altogether, and just call it “programming.”
It’s as if when we drive, we have to point out it’s “car driving” as opposed to a horse and buggy. Very few people use horses and buggies anymore, and so it’s rare that we would see the need to even point out that we’re using a car.
So on to what it is!
What is OOP in everyday concepts and plain English Language?
Pick something in your house, something unrelated to a computer itself. How about a pen?
How would you describe a pen? Specifically, how would describe the general concept of a pen, rather than the particular one you might have on your desk? What properties does a pen have?
Let’s list a few; see if you can think of a few more:
- Brand name
- Ink color
- Type of tip
Can you think of others? Take a moment to think about a few.
Those are ways to describe a pen. Notice I haven’t yet provided specifics such as “blue” for ink color. I’m just mentioning that a pen has an ink color.
Next, what can a pen do?
Well, it can write. But let’s pause for a moment on that one. When I run my pen across a sheet of paper, what is happening? Am I pulling ink out of the pen with my finger and pushing it onto the paper? Nope. The pen takes care of that part for me. In other words, the pen itself does the writing. I move it; the pen writes.
What else can a pen do?
Not a lot, but some pens can be refilled with more ink. So let’s include that one. But again, let’s look at it from the perspective of the pen. We’re kind of splitting hairs here, but imagine from the pen’s perspective we hand it the ink and it refills itself. That’s not exactly true, but we want to keep it from the pen’s perspective as if it’s doing the work.
Oh and by the way, since we’re refilling the ink, the ink level goes from 0 back to some full amount. So let’s add that to our earlier list of properties.
So here’s what we have so far:
Pen Properties:
- Brand name
- Ink color
- Type of tip
- Ink Amount
Pen Abilities:
- Write
- Refill
Great! Now we have a type of thing, in this case, a pen. But we don’t yet have a specific pen. Let’s make a couple of actual pens. How do we do that? Well, we have to fill in the actual values for the properties.
Here are three different instances of this thing called a pen:
1st pen:
Brand name: Bic
Ink Color: Blue
Type of tip: Ballpoint
Ink Amount: 5
2nd pen:
Brand name: Paper Mate
Ink Color: Blue
Type of tip: Felt
Ink Amount: 8
3rd pen:
Brand name: Paper Mate
Ink Color: Red
Type of tip: Ballpoint
Ink Amount: 3
Pause for a moment, though. Why did I not list their abilities separately? Because they all share the same abilities. We could certainly list them (Write, Refill) but we wouldn’t provide a value. We would just say that each of them can write, and each can refill itself.
Sidebar: But wait!
Let’s consider a caveat. Different types of pens might perform some of these abilities differently. For example, some ballpoint pens can’t be refilled at all. But some can; you pull the cartridge out and put a new one in. But some older style pens have a chamber inside of them that you literally pour ink into.
In other words, we could in fact provide a value for the abilities, specifically a description or series of steps on how that’s performed.
But let’s keep it simple! We’re going to write code soon and we don’t want to overcomplicate it. So let’s just treat it as every pen writes the same way, and every pen refills itself the same way. Later in another article we’ll talk about more advanced topics like different pens having different ways of performing their abilities.
Staying on the same page with the language
Before we created individual examples or instances of a pen, we just had a concept in general of a pen. Not a particular pen, just a pen. In the real world, we might use language like, “A pen is a type of thing.” And certainly we humans know about all sorts of “types of things.” There’s a glass on my desk. A glass is another type of thing, and on my desk is a particular example of a glass. It’s an instance of this concept called glass.
Although people don’t usually use the word “instance” as I’ve been using, I think most people probably understand the concept. (No kidding, I actually did it once; I held up a pen in front of somebody who isn’t a programmer and said, “This is an instance of what type of thing?” She looked baffled and hesitantly said, “A pen?” I said, “Yup! Thank you!” And I walked away, leaving her completely bewildered and wondering if it was a silent cry for help.” Fortunately she didn’t call Human Resources.)
In programming, that’s the word we use, “instance.”
And in programming we also use the word “object” to mean the same thing.
Two words for the same thing? Yep. It is what it is. Learn both. And you can use either.
But what about this concept of a thing? For that we use the word “class.” This word throws people sometimes. A class? Like a group of students? Nope. It’s a shorthand for the word “classification.” We hear it occasionally. Oxford dictionary says the word means this:
“A set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality.”
So those are two new words:
A type of thing we call a class.
A specific example of a class we call an instance.
So this red ballpoint Bic on my desk is an instance of the class called Pen.
But how do we refer to that specific one? Let’s say there are three pens on my desk and I want you to grab that particular one for me? I’ll probably refer to it just like that: “The red ballpoint one that says Bic on it.”
Would I name it?
Maybe some folks do. But most of us don’t.
However, I might store it somewhere and instead refer to it that way:
Grab the pen that’s over there in my jacket’s left pocket.
In terms of programming, that would be a good way to do it. Remember, variables are like little storage bins. And there I have a storage bin called Jacket’s Left Pocket. Hold that thought for now and let’s try out some actual programming examples. Let’s start with Java.
Tip: In the Java example, I’ll give all the notes and explanations, so even if you’re only interested in Python, please read the following Java stuff first.
Java Example of a Pen Class and a Few Pen Instances
Before we can use a pen, we need to specify what exactly a pen is – that is has a Brand name, an Ink color, a Type of tip, and an Ink Amount. But these will become variables, so we’ll want to use a name that’s legal in Java (such as brandName) and we need to provide a type (such as string or int).
Here’s part of the class for now. I’m going to add more to it shortly, though:
public class Pen {
public String brandName;
public String inkColor;
public String tipType;
public int inkAmount;
}
Look at the first line. We have the word “class” and the word “Pen.” We’re creating a class called Pen! (Don’t worry about the word public. That will come later. For now just remember to include it.)
The open curly bracket (what I call “curlies”) starts the stuff that makes up the class. Notice the matching closing curly at the end.
In between the curlies are the properties we talked about, the member variables. Each one includes a type name, in this case String or int. And each one includes the name.
Great! We just defined a type of thing, a class called Pen.
Remember, we haven’t yet actually created an actual pen. We haven’t created an instance of this class called Pen. We’ll do that shortly, but first let’s create the two things a pen can do: write and refill.
But let’s not get too advanced! We’re not ready to create an actual drawing app. Instead, let’s just think conceptually. When a pen draws, ink goes onto a sheet of paper. (And we haven’t created a class called Paper, so for real, let’s keep it simple!) From the pen’s perspective only, what happens when it writes? The ink level decreases!
So let’s start with that. We’ll write a member function (that is, a function that’s inside the class) called “write” that decreases the ink level by 1. That’s all. It’s good enough for now. Here goes; this is the whole class now, but with our new member function added:
public class Pen {
public String brandName;
public String inkColor;
public String tipType;
public int inkAmount;
public void write() {
inkAmount--;
}
}
Look at the contents of the function. All it does is decrease the value that’s stored in inkAmount.
Now let’s add the refill function! This one is also quick and easy. We just want to return the inkAmount back to 10.
public class Pen {
public String brandName;
public String inkColor;
public String tipType;
public int inkAmount;
public void write() {
inkAmount--;
}
public void refill() {
inkAmount = 10;
}
}
And that’s it! The refill function stores a 10 in the inkAmount member variable.
Now pause for an important question: In this class we only have on brandName, one inkAmount, and so on. Does that matter? Nope:
I can create a whole bunch of pens, and each will get its own brandName, inkColor, tipType, and inkAmount. And when I call the write and refill functions, those functions will modify the inkAmount for the particular pen I refer to.
So let’s see it in action. First we’ll look at how to create a single pen, set the values for its member variables, and call its write and refill methods. Then we’ll create another pen. (To see the full code and their files, you’ll want to check out the repo; I have a link to at the end of this section.)
Here’s an example main that uses the Pen class:
public static void main(String[] args) {
Pen mypen = new Pen();
mypen.brandName = "Bic";
mypen.inkColor = "Red";
mypen.tipType = "Ballpoint";
mypen.inkAmount = 10;
System.out.println(mypen.inkAmount);
mypen.write();
System.out.println(mypen.inkAmount);
mypen.refill();
System.out.println(mypen.inkAmount);
}
Look at the first line inside the main, and notice how I’m creating a variable called mypen. But what is the variable’s type? It’s Pen. That’s the name of the class. And it’s true: When you create a class, it becomes a new type you can use in your programs.
And what am I storing in the mypen variable? On the right hand side is this: new Pen(). This is a little different from other variables. If I create an integer called n, and I put a 15 in it, I just type this:
int n = 15;
But with classes, we have to use the word “new” and then we type the name of the type, followed by an open-paren, close-paren. This tells Java to create an empty instance of our class. And by “empty” I mean that the brandName and all that isn’t filled in yet.
Then look at the next four lines. You can probably see that I’m setting mypen’s brandName variable to the value of “Bic”. But look how I’m doing it. I type the name of the variable holding the pen, followed by a dot, followed by the member variable I want to use. Then comes the usual equal sign followed by the value.
That’s how we do it with classes:
To specify a member variable: We type the name of the variable holding the object, then a dot, then the object’s variable name.
After we fill in the member variable names, we print out the value of InkAmount. It prints 10, which is what we set it to.
Then we call the member function called write. Again, we follow the same format. For this format I like to say “noun dot verb.” The noun is the variable holding the object, in this case mypen, and the verb is the member function (sometimes called method) that we call, write. And like any other function, we put an open-paren, close-paren. (Can we include parameters in our member functions? Sure! We just aren’t here.)
Then we print out the inkAmount again. The first time we printed it out, it was 10, which is what we stored in it. But after calling write(), which decreased the value by 1, now we print out 9.
Then we call our refill() method, which stores a 10 back in inkAmount. And sure enough, when we next print out the value, we see it’s back to 10.
Here’s the output:
10
9
10
Awesome! Now let’s create a second pen. For this one I won’t provide a running commentary. Instead, try out the code, and notice that when we provide the name of the variable holding the particular instance of Pen, followed by a dot, that’s the variable we use.
So when I type
mypen.refill()
I’m calling refill for the instance in mypen. And when I type:
secondpen.refill()
I’m calling refill for the instance in secondpen.
So the first call sets the first instance’s inkAmount, and the second call sets the second instance’s inkAmount. See how Java keeps all that straight?
Try it out!
Ready to try it out? Check out our repo, which includes instructions for how to clone it! Here’s the repo: [full link, not just the word “repo” linked to it]
When you look at the repo notice a couple of things:
The Pen class goes in its own file, and the name of the file matches the name of the class. In Java, this is required. (Most other languages it’s not. Other languages the filename is irrelevant.)
Next, I created a folder structure. And that folder structure is used in the import statement. (As for the name of the folder structure. I used my domain name, excodiur.com, but in reverse. That’s also typical. The idea is that the domain name is your business name, and that becomes your “package” name.)
Other Languages
Other languages work very similarly. Why? Because they’re all essentially descendants of the original object-oriented programming languages. And the concepts are all the same.
Python has a strange (and admittedly annoying) nuance where every member function has to have the word “this” as a parameter, and before you can access the member variable, you have to precede it with that same word this.
And JavaScript has its own strangeness. (JavaScript technically has a radically different way of handling classes called prototyping. You can read about it here: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes
C# looks very much like Java since they’re sort of “sibling” languages.
C++ is one of the original OOP languages, yet you can see it has a lot in common with Java and C#.
Here are the repos where you can find the code for these other languages:
- Python
- JavaScript
- C#
- C++
Next Steps
You can probably think of a lot of things that are missing. For example, right now you can pick any ink color you want. How about “Wolfgang Amadeus Mozart” for the ink color? Or maybe “AHUILAGFJAKLD”? Exactly. We probably don’t want to be able to pick just any string for the ink color. And how about a negative value for the ink color?
We don’t want to be able to pick just any random value for our member variables. (Yeah, that’s the official term for the properties.)
And should we really be able to set the ink level directly? Really, writing should decrease the ink level, and refilling should set the ink level, and that’s it. A person can’t just grab a pen and say, “You shall now have an ink level of 3!” and so it is done.
And while we’re nitpicking, shouldn’t we provide an easy way to start out with a pen, making sure the brand is set, the ink color is set, the tip type is set, and the ink level is set? And for that matter, shouldn’t a new pen start with ink level of 10?
Yes to all of the above. We’ll tackle all of this in another article.(Hint: For that last one, about the pen starting out, we’re creating, building, or constructing, a new pen. And the programming term is that last one, a “constructor.”)
About Author
jeffrey
My name is Jeffrey Cogswell and I've written and taught for 30 years and worked full time as a developer in languages such as Python, C#, Java, and C++.