For those of you who have your feet somewhat wet in the ocean of programming, surely you must’ve come across the concept of Object-Oriented Programming, or in short, OOP. What is it? How to best utilize it? What is the hype around it all about?
I would be utilizing examples in the deeper levels with the help of the programming language Java (an object-oriented language). Granted, there are plenty of object-oriented languages out there (C++, Python, .Net, Ruby etc), so feel free to use this post to wrap your head around the concept of discussion, as you would still be able to get a hang of it, regardless of the language being used.
LEVEL 1 – CORE CONCEPT
Imagine you’re a girl scout running a cookie business. Being a girl scout, of course, you’ve decided to shape your cookies with the shapes circle, square, and star-shaped. To do this, you get yourself some ingredients, a cookie cutter, and got to work.
The concept of OOP revolves around two things – class and object.
In the girl scout cookie analogy, the cookie cutter is the class, and the resulting shaped cookie is the object. Stay with me here.
The class can be seen as the template for creating an object. It’s a blueprint for the object, but it isn’t exactly the object itself (just like how the cookie cutter shapes the cookie, but it isn’t the cookie itself). You can’t actually smell, taste, or feel the cookie in your hand until you’ve created it using the cookie cutter.
The cookie cutter is there to create cookies. The round cutter creates round-shaped cookies; the square cutter creates square-shaped cookies; the star-shaped cutter creates star-shaped cookies.
Speaking in terms of programming terminologies, the classes create objects.
Cookie cutter is NOT the cookie. Cookie cutter creates cookie.
Class is NOT the object. Class creates object.
LEVEL 2 – DEEPER DIVE
Now that we’ve established the relationship between classes and objects, let’s look into a little deeper into what constitutes an object.
Think of everyday objects. A cat? A table? A cake? A car? How would you describe a car to someone who has no idea what a car is? You might say something like:
- Made of metal
- Has 4 wheels
- Can be driven
- Has head lights
- Blue color
An object can be described with two things – what it can do, and how it looks/feels/tastes/sound/smell like. Or in other words, objects have methods and properties.
Methods are the things that the object could do. If you were actually creating cookies like those in the movie Despicable Me, you might include methods like transform(), walk(), shoot(), explode() etc. (Methods are usually named with a full bracket behind their name).
Properties, on the other hand, are characteristics. Basically, adjectives used to describe the object. Some of the properties of the cookie object might include chocolate flavored, strawberry flavored, peanut butter flavored, sweet, crunchy, goo-ey etc.
LEVEL 3 – NITTY GRITTY DETAILS
Now, we’re going to attempt to create a class of our own using the programming language Java. If you haven’t coded in Java before, I would recommend you to at least go through the basics of its syntax before proceeding.
I’ll be using the IDE Eclipse btw.
First, create a Java Project.
Then, time to create a class in the project we’ve just created.
Here, I’ve created two classes – girlScout and cookie, with the main() sitting in the girlScout class. Not much to get excited about here.
What we’re going to do now, is to create a method in the cookie class so it could do something.
Great! Now we have a class cookie with the method greeting() in it. Now, to actually use the method, we’re gonna have to create the cookie itself. Remember, the class is the cookie cutter, while the object is the cookie itself. The class can only create the object, but not do anything the object can (you can eat a cookie, but not the cookie cutter).
The process of creating the cookie, in technical programming terms, is called creating an instance of the class, or in simple terms, creating the object of the class.
To do so, do this:
What the line cookie round_cookie = new cookie() essentially means, is:
Using the class cookie, create a new round_cookie.
P.S. the cookie() method at the end of the ‘=’ sign is the constructor of the class cookie. A constructor is basically just another method but is named after the name of the class itself. It’s given the term “constructor” only because it’s executed first automatically whenever you create an object of the class. Nothing special.
If you try running the program, you should see absolutely no change at all. But beneath the surface, you’ve just created an object. To use the method, you’ve gotta access it through the object as follow:
Run it again, and you should get:
Recap: What have you just done?
You’ve created a round cookie using the cookie class, and made it greet “Hi I’m a cookie”.
“Neat. So what?”, I heard you say.
If you’re not convinced of the power of object-oriented programming even at this point, here’s an example of it.
With classes, you can do a neat little trick called inheritance. Look at the items below:
- The Porsche 718 Boxster that I own.
- Porsche the car brand.
- Car, the category of a vehicle
- Vehicle, the category of item.
A vehicle has wheels. A vehicle can be driven. But so does a car, and so does a Porsche, and so does my version of a Porsche. Instead of having to explicitly define the ability to be driven for each class, a class could simply inherit the ability from its parent class or super class(just like how you inherited some characteristics from your parents).
My 2017 Porsche 718 Boxster<-(is a model of)<-Porsche<-(is a type of)<-Car<-(is a type of)<-Vehicle
Hence, you could define some properties and methods that a parent class could have, and inherit them to their daughter classes, instead of having to explicitly define each of them at each level of class.
Instead of having to explicitly define the ability to be driven for each class, a class could simply inherit the ability from its parent class
Let’s look at the codes below:
1. First, we have the class Car, which defines out the methods of a Car:
2. Then, we have the class Porsche, which inherits from the class Car. This is done using the keyword extend
3. Finally, time to see things in action. For this, we’ll create a class, and create an object of the class Porsche in it to see things in action.
4. Here’s the result:
Get the code here.
This is just one of the cool things OOP could achieve. In addition to this, there’re other concepts such as Polymorphism, Abstraction, and Encapsulation – all of which if mastered, could prove to be an invaluable tool at dispense.
– Just another dude