Quick Introduction to Object Oriented Programming

David Morales
Jan 24 · 5 min read
Lego pieces

The object oriented programming concept is rather old, it first appeared in the 1970s from the Smalltalk language, and it describes the use of objects and messages in a computer program. Basically the objects are the actors of the movie (the program), and the messages form the script.

The most popular languages used for web development support object oriented programming (OOP), being Ruby a pure object oriented language from the beginning. So I will use it to show you the OOP concepts in this article.

Class

We can understand a class as something that describes a concept. For instance, let’s suppose we have a computer class that describes how a computer is. I will use Von Neumann’s architecture:

Von Neumann’s architecture
Von Neumann’s architecture
Von Neumann’s architecture

Essentially (and to make things simple) a computer should have a CPU, memory, and input/output devices. Let’s write a class:

The parts of our generic computer are called properties. The initialize method is the constructor, because it’s called automatically when the class is instantiated (more on this in the next section). The to_s method is special as well, you will see why in the next section.

Object

Because a class is only describing something, we need to instantiate it into an object to actually use that description.

We already have a computer description, we know how a computer is. So if we buy one, we will have our computer. Let’s buy it:

Yay! Now we can inspect our computer:

When defining the to_s method, it will be called automatically by Ruby when displaying the object itself. So in this case it will be called and it will display this information:

Yeah, we have a generic computer. But no one has a generic computer. Every computer has computer parts built by a brand. And that brings us to the next concept.

Inheritance

This concept can be understood as a relationship between two classes. One is the parent, and the other one is the child.

In Ruby, a parent can have multiple children, but a child can only have one parent. There are other languages where a child can have multiple parents (multi-inheritance).

The child will inherit all properties and methods from the parent.

Let’s build a Macbook Pro:

Here we have inherited everything from the Computer class, but we have defined the initialize method again. This action is known as override, we have overridden the constructor.

Now when we instantiate an object and display it, the to_s method will be called, and because we have not overridden it, the Computer code will be taken.

From this point we could customize our Macbook Pro adding more input and output devices, but let’s keep it simple.

Ruby has a mechanism that can be used to replicate multi-inheritance: modules. So let’s create a module for portable computers:

Now I will include the brand new PortableComputer module, initialize the battery property and override the to_s method of our Macbook Pro class:

Hey wait! What’s that super thing? It is used to get the parent’s code. So in this case I’m getting all the to_s code, and then appending the battery string.

Running our code again should display this:

Encapsulation

Did you notice how we displayed the specs? We just called puts followed by the object. Ruby called to_s automatically and the specs were displayed.

The way we built the output string inside the method gives the class the encapsulation concept, because when we use puts, we don’t know how to display that information, we just call the object and that’s it. It’s the responsibility of the class to do it, so using the object is simple and straightforward.

So encapsulation can be understood as hiding the internal representation of the object (our to_s method implementation) from the outside (when using the object).

Let’s suppose that Apple let us to upgrade our Macbook Pro’s memory. This can be achieved by implementing a new method in the class:

Now from the outside we can upgrade memory just calling that method. We don’t need to know how to do it, so we achieved encapsulation. Let’s see it in action:

It should display 16 GB of memory now.

Polymorphism

Polymorphism is when we provide a common action for different classes. While the action is apparently the same, it has a different implementation for each class.

Let’s come back to our example and tweak our Macbook Pro’s upgrade_memory method:

Now let’s create a new computer, a 27-inch iMac:

Instantiate and call the objects like this:

This is the output:

The computer specs are from our iMac. We can see that memory was successfully upgraded. But then we could not upgrade our Macbook Pro’s memory. It’s displaying our error message, and then the specs still show 8 GB.

Conclusion

I encourage you to modify the sample code I have used for this article. Play with it and try to break things. It’s a good way to consolidate your new knowledge and understand it even better.

Although I have used Ruby, the concepts are basic and are the same for the rest of languages.

The Startup

Get smarter at building your thing. Join The Startup’s +793K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

David Morales

Written by

Computer engineer. Working as a web developer since 2000.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +793K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store