An OOP Story

Every time I am teaching the OOP concepts, I always tell this story to my audiences. Since my first class back in 2008 until today. Without fail. The story is applicable to any programming languages.

This is a story about class, object and constructor. The story begins like this.

Imagine you have a piece of empty land and you’re planning to build a house. A bungalow. Since most probably you can’t draw the plan yourself, you might need to hire an architect for that.

During the first few meetings, the architect will start asking you lots of questions;

  • What kind of bungalow you’re planning to build? 1-storey? 2 or 3 maybe?
  • How many rooms/bathrooms/kitchen etc?
  • What materials for the windows/doors/floors etc?
  • And many more questions.

Only after he or she has gathered enough informations, he/she will go back to his/her office and start drawing. Of course, from time to time, he/she will give you a call or ask for a meeting to clarify more things and also to get your feedbacks. Once the blueprints are ready and signed-off, now it is time to start building the house.

The next question is; can the architect build the actual house? (Oh, maybe he can but let’s just assume in this story, the architect just draw the blueprints). Now, your job is to find a contractor that can build the actual bungalow.

After a tedious process of acquiring a liable and reliable contrator, the construction of your dream bungalow begins. How the contractor build it? Exactly as drawn in the blueprints.

Now, this is where I usually take a break from the story.

You see, object-oriented programming (OOP) borrowed lots of concepts from us, human in real-life.

First, look at Class. Class by definition is a blueprint, or template that defines or descibes the object to be created. In our story just now, class is similar to the blueprints prepared by the architect; it contains all the requirements collected earlier and clearly defined how each room in your actual bungalow will look like.

Second is Object. Object is defined as an instance of a class. In our story, object is the actual bungalow that you’re going to build. The process of building it is called construction – or instantiation in programming term.

Finally, the Constructor. The name couldn’t be more apt. Contractor and Constructor. Contractor build the bungalow, while Constructor instantiate (create/construct/build) objects in program.

Did you get it now?

Now let’s go back to our story. Your bungalow is now completed, you are happy with it, decorated to your likes and moved in. You are planning to hold a housewarming party to show off. You booked caterer and whatnot and on the day itself; your families and friends came to visit. They were so impressed with the design and want to build their own bungalow with the same design. Question is – is it possible?

Of course it is possible. All they need to do is buy another piece of land, hire a contractor (or maybe use the same contractor) to built another bungalow with the same design i.e. using the same blueprints. At the end of the day, there will be two bungalows that look very much alike, but maybe have different wall colors, windows, doors, floors etc. Another thing that is different is the addresses of the bungalows. There’s no way you can build both bungalows on the same lot i.e. having the same address, right?

It goes the same with objects. Once you’ve defined a class, you can create as many objects from the same class. These objects are created and stored in memory, with each object having its own reference known as memory address. Since these objects were created based on the same class definition, they will have the same set of properties but each properties may stored different values. Just like our two bungalows; one might be white-coloured and the other one is blue-coloured with different floor, doors and windows materials.

Okay, that’s the end of my story.