I’m working with a couple of different groups on various projects to learn and teach two related Object-Oriented Programming (OOP) languages: Apex and Java.
One of the big stumbling blocks for beginners is understanding OOP terminology and concepts. In this blog post, I’m taking the opportunity to gain better understanding of the terminology, by explaining it to you.
This post is also for my dad, who (brave man that he is) edits my blog without any background in programming, and without, I’m guessing, knowing what the heck I’m talking about half of the time. Now that’s love!
This is the first in a series of blog entries on OOP concepts and I thought I’d define some of the big ticket items: classes, variables, methods, and objects.
The easiest way to understand classes is to think of them as templates for the “stuff” (or objects) that we’re working with. As I love cooking, and this year bought a beautiful new oven, I’m going to use the idea of the oven for this example.
When thinking about classes, it’s helpful to think about the objects that you’ll be creating from the class and ask yourself two questions: a) what does it know about itself (variables), and b) what can it do (methods)?
In our oven example, when we think about our Oven class, and ask our two questions, we might come up with the following:
- What it knows about itself (variables):
- I have four burners and a broiler, all initially set to the “off” position
- I have an oven space, where the temperature is initially set to “zero” (in reality probably room temperature)
- I have one storage drawer
- I am white on the outside and black on the inside
- I have an oven light
- I have a timer
- I have a brand and cost
- What it can do (methods):
- I can turn my burners or broiler on or off
- I can set the temperature for my oven space
- I can close and open my oven door
- I can play a tone when various things happen, e.g.:
- Beep three times timer go off and repeat until timer is turned off
- Beep five times when the set temp is reached
There are lots more variables and methods you could create. Take a couple minutes to make up your own. (Hint: what about lights, exhaust fans, internal oven fans, gas or electric)
You’ll notice that this Oven class has some default values for various variables, but not for others. For example, the base oven has a default oven temperature of zero and has a storage drawer. It also knows that it has a brand but it doesn’t, by default, know what that is (yet). That will be defined when creating (an instance of) the object.
The trick is, that this class isn’t an actual oven, but rather the blueprint or idea for what an oven is and what it can do. It’s like the difference between thinking about an oven (class) and having a specific oven in front of you (object) that you can touch and use.
You could say, that when I was looking for a new oven, to the basic features of an oven through my research about the oven template, aka the Oven class. What I was looking at when I got to the actual store and saw all the various ovens, were oven objects based on the Oven class.
After looking at all the various options (that I could afford) I selected a specific oven (create an instance of a new object) called myBeautifulOven, which:
- Has five burners, which all start set to the “off” position
- Has an oven temperature that starts at zero
- Has a broiler, which defaults to “off”
- Has one storage drawer
- Is made by LG
- And, no joke, is blue inside the oven and has different ring tones for different status (e.g., when the oven reaches the set temperatures, etc.)
When we created (instantiated) our object we can override the default values. For example, myBeautifulOven object has:
- Five burners instead of four
- A brand of “LG”
- A startling blue oven interior (apparently to more accurately see the “doneness” of food)
- An identity complex; it thinks it’s a cell phone, and sings songs to me when the timer goes off instead of just beeping
A new object can also inherit variables “as is” from its class, for example, myBeautifulOven has:
- A storage drawer
- A broiler
- A timer
- An oven light
Regardless, the values of our object’s variables may change over time. For example, we may turn on the oven by using the setTemperature() method and thus the value of our ovenTemperature variable will increase.
Note: You can generally tell something is a method, because it has open and closed parentheses after its name, e.g., openOvenDoor(). We’ll talk more about this next.
More about methods
I’ve mentioned that classes have variables (boxes to store what objects know about themselves) and methods (things objects can do).
In the case of our Oven class we have some pretty basic methods that, once we’ve created the object, we need only reference (call) to use. For example, we can open or close the oven doors with methods such as openOvenDoor() and closeOvenDoor().
We also have methods that need information (arguments) passed to them in order to work. For example, we can’t turn on the oven without setting a temperature, or use the timer without saying how much time we want to set. We give methods these values (pass methods arguments) within the parenthesis at the end of the method name. For example:
- setOvenTemp(350) would turn on the oven and set the temperature to 350°F.
- setTimer(0,20,30) would set the timer to be 0 hours, 20 minutes and 30 seconds.
Like any well named method, you can guess what it does. You can’t, however, know for sure everything that’s going on in the method, but that’s part of the joy of OOP, you don’t need to know that in order to use it. You just need to know that the setTimer() method takes three number values (arguments), one each for hour, minute and second. You don’t need to know that it also sets the song to sing at the end… or that it repeats until the timer button is turned off!
Don’t worry, understanding all this is not going to be accomplished in this one blog post (I’m good, but not that good!). The goal of this post was to explain four basic concepts of OOP: classes, variables, methods and objects.
It’s okay for now that you don’t know how to use them; that will come in a later post.
Wrapping things up
A lot more can be said about these basic OOP concepts, but hopefully this has given you a basic understanding of how to think about them. To recap, we now know the following:
A Class is a template for an object (e.g., template for an oven) including:
- Variables—What the objects created from the class know about themselves.
- Methods—What these objects can do, and may or may not need information, or arguments, passed to them in order to do their jobs.
An Object is the actual thing that we can use, and is created, or instantiated, based on a particular class. They can accept default values defined in the class, or override them.
So thinking about the everyday objects that you have in your home, see if you can apply the same two questions to them. If you were to create a class for your TV, fridge or vacuum, what variables and methods might you come up with? What other objects can you find to create a class (template) for? Post your examples in the comments section…I’d love to see what you come up with!
I hope this has helped demystify the basic building blocks of OOP. This is the first post in a series that will explain basic OOP concepts and then move on to Apex programming as an example.
In case you’re curious, this is one of the songs my LG plays