Quick introduction to object oriented programming

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, 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 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 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 reality 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 codes I have used for this article. Play with it and try to break things, as it is 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.

Did you like it? Please share it:

Get my ebook for free

10 ideas that helped me become a better developer (and may help you too)

Subscribe to my mailing list and get my ebook on 10 ideas that helped me become a better developer.

About Me

David Morales

David Morales

I'm David Morales, a computer engineer from Barcelona, working on projects using Ruby on Rails and training on web technologies.

Learn More