Wading into uncharted territory is half of the fun of programming. Being able to take an idea, or a thought, and expand it into results is as gratifying a feeling as any. I will concede, however, that it can also be daunting. If you checked out my earlier post about coding terms then hopefully you feel confident thus far with the basic concepts. One thing you will find as you go deeper into programming is that everything revolves around that word – concept.
Code is not inherently tangible. That is to say, you can’t touch code. You can’t smell it or weigh it. Code is merely a formal way of describing informal ideas and concepts (there’s that word again). If I were a woodworker and I wanted to make a canoe, I could go to the store and buy a piece of wood. I could feel it, I could tap on it, I could sand it – I could physically inspect and mold what the wood is into what it is going to become. But can I do this with code?
Programming for “Real” Digital Life
The answer is that we can do this, albiet in an abstract sense of the idea. I can’t buy a big hunk of code and then sand it into a canoe, but I can use my code to describe the canoe. I can say how long it is, how much it weighs, how many people can fit in it, and what color it is. What you will come to find is that lots of programming isn’t so much about creating as it is about describing. We don’ t create things with our code, we simply describe what those things could and should do. Using the example I gave, we aren’t making a canoe, but we can do a lot to describe what a canoe is like.
This sort of programming is called object-oriented programming. Many languages are classified as object-oriented programming languages. Languages of this type include Python, Java, C++, and many others.
What is object-oriented programming?
Object-oriented programming (called OOP) is a style of programming where the focus is on creating digital representations of real-world objects and concepts. That is very wordy, so picture it like this: Imagine you have a friend who has never eaten pizza before. After your initial disbelief, he asks that you explain to him what pizza is. So what could you tell him?
You could describe what ingredients go into a pizza, what temperature it cooks at, what shape it is, how many people it could feed – the list goes on. By describing the pizza to your friend, you are essentially forming his concept of what a pizza is. He will know what you tell him about pizzas, no more and no less.
Object-oriented programming applies this same concept to computers. A computer doesn’t exist out and about in the real world as a human does, so its knowledge of things is limited to what we tell it. In OOP we are focused on describing real world objects to our computer so that it can make sense of what we are trying to accomplish. The important piece to note is that a computer’s knowledge of an object is based entirely off of what we tell it. I could tell the computer that a pizza is a square made of sand and crayons and that the most popular topping is used marbles and it would be none the wiser.
The Components of Object-Oriented Programming
Now that we have an idea of the general concept of object-oriented programming, let’s get into the real meat of things. We say that we use objects in coding so that we can describe an actual thing, like a car, a pizza, or a dog – but there are millions of things out in the world! Even in our program, we might only use a few dozen objects, but even that seems like a daunting task. Do we really have to sit there and tell our computer every detail about every object we want to use? Of course not. Get ready, because here comes another concept…
Imagine you are writing a program where we want to compare a few cars and decide which one gets the best bang for your buck. Let’s say we have three objects we want to use: a Corolla, a Civic, and a Mustang. Remembering that our computer has no idea what a car is, we would have to describe each of the cars through our code. Before we sit down and start explaining each of the cars though, we might realize something – the three cars have a lot of stuff in common. Each car has wheels, doors, and windows, as well as a miles per gallon rating, a horsepower rating, a color, headlights – the list goes on. This is where the concept of classes comes into play.
A class is a sort of blueprint from which we can create similar objects. We can create a class called Car, and describe what the general idea of a car is to our computer. We can say that any car would have wheels, doors, an engine, seats, and so on. By making the Car class we are telling the program that there are going to be objects that use this basic blueprint, and that each object can differ a bit from one another. From this Car class, we can create our three car objects: the Corolla, the Mustang, and the Civic. Because the computer has an idea what a car is, it will imagine each of the three as having certain characteristics. Each of these characteristics is called an attribute. Attributes for the Car class could be things like how many wheels the car has, what color it is, what its horsepower is, etc.
In case if you were wondering, I keep capitalizing Car because the naming convention (norms) for naming a class is to capitalize the first letter in the class name. This makes it easier for others reading our code to know where the classes are described.
As we happily create our Car class and then instantiate our Car objects (instantiate is a big word meaning to declare that we are creating something), we might wonder what to do with these classes. I mean, its great that we were able to describe cars and then make different cars based off of the blueprint, but what do they do? In real life, a car can do all sorts of things, like go forward or backwards or turn its lights on or off – how do we describe that stuff in code?
Method(s) to the Madness
Here we introduce a concept called methods. A method is a piece of code that we write which tells an object to accomplish something. I purposely make that sound very general, because what a method does is really defined by what we say it does. For our Car class we might have methods to do things such as drive, switch gears, or turn on the radio. Methods are very helpful because it saves us the trouble of needing to constantly update the program and tell it what an object should be doing. Object-oriented programming allows us to use methods as a critical time-saving tool. We can write the idea of what a method is one time – what attributes are involved, what should happen, what the end result of the method should be – and then instead of writing those instructions over and over, we just use use the method’s name in our code whenever we want that same thing to happen again. This is referred to as a method call, meaning that we are telling the program “Hey see this method name I just wrote? Call up that method, ask it for what it does, and then do that here”.
Like most things in programming, there is a naming convention for methods. The convention is that if the name is more than one word, we keep the first word lowercase and capitalize each subsequent word. For example, if I had a method that was used to tell my program to turn on the radio in my car, I could call it turnOnRadio.
Functions Vs. Methods: The Reckoning
There is another concept in object-oriented programming that is very similar to a method, and that is the concept of a function. A function is more or less the same as a method in theory, but the difference is that a function is sort of free floating whereas a method applies to a specific Class object. This might be a bit difficult to understand right now, but just think of it as a method being a set of instructions that apply to a specific object (I.e. my turnOnRadio method applies to a Car type object) whereas a function does not need to be tied to an object. In essence, a method is something that we say “I want this to be something that only certain types of objects can do” and a function is something where we are saying “I want this to do something, but I don’t need a specific object to be the one to do it”. If you are still having trouble understanding the difference, don’t worry at this stage. The difference between methods and functions is relatively minor, and will be much easier to understand once we see it in practice.
What Have We Learned?
When we combine these ideas – classes, objects, methods, and functions – we can understand the essence of what makes object-oriented programming so neat. Coding is a very formal process by nature, but OOP allows us to describe more informal ideas through our code. Instead of having to spend hours trying to piece together each component of what our code will need, we can use classes to make blueprints for what the coding will entail. We can then use the map that our class helps create and guide our program to create objects that have certain attributes and methods. These attributes and methods are used to mimic the qualities and abilities of those objects in real life. When we put it all together, we find that our code is perfectly capable of emulating real-life situations and scenarios. Therein lies the heart of object-oriented programming, the ability to use our described objects to act in place of its real world counterpart.
If we can grasp the concepts of objects, classes, and methods then we can understand the basic tenants of what makes object-oriented programming work so well. Obviously there is a lot more information to grasp and a lot more definitions to pore over, but I will cover all of that in future articles. The goal right now is to build up your confidence with the terminology and concepts at work, so that when we sit down and start punching in code there is nothing standing in our way. So what do you think, do you feel ready?
Skip to the Good Stuff – Definitions
Object-oriented programming – A programming style that centers around the use of objects to describe real-world scenarios.
Object – A coded (digital) representation of a real-world thing or concept.
Attribute – A quality or trait used to describe something about an object (ex. If your object was a pizza, an attribute may how many slices it has).
Method – A coded instruction that tells either tells an object to do something or tells the program to do something to the object.
Function – A coded instruction that tells the program how to make something happen.
Call – Telling our program to find the method or function we mentioned, and to execute it.