The Essentials of OOP

Joshua Singleton
5 min readApr 13, 2021

--

Object Oriented Programming, OOP is widely used in Computer Science. OOP is classified as using variables and functions in an ‘object’. These objects can be grouped together in classes. These classes will provide the structure to build applications with properties in variables and methods through functions.

There are several languages and frameworks that use OOP. Ruby, JavaScript and Java are just a few. The reason why OOP is widely used and accepted is because of the 4 core concepts that make OOP so dynamic. These concepts are Abstraction, Encapsulation, Inheritance and Polymorphism. These are techniques that programmers can use to simplify their code seamlessly.

Encapsulation

When I hear the word encapsulation I think of two things: 1. Medicine encapsulated in a pill or capsule. 2. DragonBall Z. We will focus on the medicine. Why do doctors encapsulate medication? This is so the measurements of the medicine ingredients are already weighed and allocated so that the end consumer does not have to worry about it. Another great reason is that if someone wanted to tamper with the medication they would have to break open that capsule. This is a warning for those who see the capsule open that the medication may be altered or at least not the same medication that the manufactures made in the beginning.

The same concept is applied in programming. If someone wanted to create a function that they did not want anyone else to tamper with such as end users or fellow programmers they would encapsulate that function in a private method or different file. I use this in my Ruby on Rails application. I can write code in ‘private’. This code can still be used in my application however it cannot be modified outside of private.

Encapsulated Code in Rails Application

I can use encapsulation in my React applications as well. The great benefit about using React Framework is the ability to create components with other components. Components can be describe as a collection of functions and JSX, javascript markups that can be plugged into other components. In other words, components can encapsulate methods and properties and import them in other components. This is essential for debugging your application. React provides intuitive interaction to let you know where the problem lies and what is needed to fix it.

First Child and Second Child Encapsulated
Parent Component renders First Child and Second Child Component

Abstraction

One of the first things that come to mind when I think of abstraction is abstract art. If you look up abstract art on wikipedia, you would see it described as art that is a composition of different yet independent elements. We can use this concept in programming. We can use abstraction in languages such as JavaScript by creating a function that is made up of smaller specific functions.

How do you know when to abstract your code? There are two rules of thumb for me personally. If it takes up a considerable amount of lines, there may be opportunity to abstract some of that code in a smaller function else where and call that function inside. Another rule of thumb is that if you as the creator of the code cannot understand it… you may need to find ways to abstract your code into smaller chunks.

Abstraction and encapsulation work hand in hand. In the example above one of the child components encapsulates an input field. The parent component is an abstraction of the children components.

Inheritance

Do you notice your inherited traits from your parents? Genetic makeup, body attributes, quirks or movement? More importantly, have you ever inherited fortune from a loved one that has passed away? If so let’s talk.

Just like with physical inheritance, you can structure your code with hereditary-like functions that are available for every instance of your class. This concept of OOP follows the dry (Don’t Repeat Yourself). You can just create an instance (copy) of your class and build further. Maybe you want to create a new class that extends from another class. You can do this and more with Inheritance. Take a look at my example below.

Refer to my other article on Inheritance for more information!

Polymorphism

You do not have to be a martian to use Polymorphism. This is an OOP concept that allows you to code using the same words but to have multiple meanings in different situations. This can be done in two ways: Overloading and Overriding.

Overriding is when different meaning are implied by the code itself. In overriding, the child class can use polymorphism to override a method of its parent class. You may want to use polymorphism if you want to make a child class special, unique or used for a particular situation.

Overloading occurs when a single method may perform different functions depending on the context in which it is called. This can be determined by what arguments are passed in the function.

These four concepts are the main pillars that uphold OOP. If you have or are using an Object Oriented Programming language you may have been practicing one or more of these concepts. It is nice to put a name to the process. How can you further build upon these concept to create an application that is unique, safe and practical?

--

--

Joshua Singleton

Student of Code in the hopes of creating a new social network to enhance the community!