Understanding Object-Oriented Programming (OOP) in 3 levels

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.


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.

One cookie got eaten obviously. Gotta stay realistic.


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.


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
  • etc

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).

giphy (4)

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.



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.

Image 1
Create a Java Project
Image 2
Just give it a name. Any name would do.


Then, time to create a class in the project we’ve just created.

Creating a class
To create a class, head over to File->New->Class


Here, I’ve created two classes – girlScout and cookie, with the main() sitting in the girlScout class. Not much to get excited about here.


Image 11
girlScout class
Image 12
cookie class

What we’re going to do now, is to create a method in the cookie class so it could do something.

Image 13
We might as well give a method greeting() to the cookie function. Whenever this function gets called, the cookie shall print out the phrase “Hi I’m a cookie”.


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:

Image 4

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:

Image 3


Run it again, and you should get:

Image 5

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:

Image 7
Let’s keep it simple. Cars can only do pretty simple things. They can drive(), honk(), and play the radio()

2. Then, we have the class Porsche, which inherits from the class Car. This is done using the keyword extend

Image 8
This can be read as “Porsche is an extension of a Car”. In essence, what’d happen is as if you’ve copied and pasted all the code that is in the class Car into the class Porsche.

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.

Image 9
Basically, the object myPorsche was created, and the method honk() was called upon it.

4. Here’s the result:


Image 10


Screenshot (131)
Here’s all the code in action side-by-side, with the console at the far left

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




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: