A New Coder’s Experience with Object Oriented Programming

weight lifting
Photo by Javier Santos Guzmán on Unsplash

What is OOP?

What is the alternative?

On the other hand, functional programming puts an emphasis on the evaluation of functions. Functional programming works most efficiently when the data is fixed and requires many operations. Data scientists frequently utilize functional programming languages when they need to manipulate large amounts of information. R is a popular functional programming language.

Fundamental Concepts of OOP

The four most important fundamental concepts of OOP are Inheritance, Abstraction, Encapsulation and Polymorphism. These concepts have ambiguous names, but I will break down each one below.

Inheritance

Inheritance allows us to create a hierarchy of classes, and each child class inherits traits from the above classes. Think about the following hierarchy.

Photo by Erik Mclean on Unsplash
Photo by Lucas Benjamin on Unsplash

Abstraction

Abstraction is the practice of turning complex problems into a simple interface. Abstraction hides unnecessary details so that the user can implement more complex logic on top of the existing logic. In this way, a user never has to see or understand the underlying code. The concept of abstraction is sort of like ordering a drink at the bar. You don’t have to be concerned about the details of what liquor is in the drink, or even how that liquor was made. You are focused on is the drink in your hand and what you will do next with it, not anything else.

Photo by Messala Ciulla on Unsplash

Encapsulation

Encapsulation is the principle of ‘protecting’ methods from the user. This permits the user to only access specific methods that are outlined to the programmer. Continuing with our bar example, a customer is permitted to order a drink at the bar. The customer is not allowed to grab bottles and pour their own drink, they must go through the bartender. There are a few ways to do that in Ruby, but the example below shows encapsulation using the ‘private’ method.

Photo by Artem Maltsev on Unsplash

Polymorphism

Polymorphism refers to the ability of an object, function, or variable to take on multiple forms depending on the input object. This may be the most complex of the four concepts to understand, but we will take it slow. Broken down, “Poly” means Many, and “morph” means form. We can tie this back to our initial vehicle example and inheritance. When a child class holds a method of the same name, the most recently called variable, function, or method will take on the most recently called form. Look at the code below:

What’s next?

These four fundamental concepts of Object Oriented Programming are just the beginning of the journey in learning new coding languages. It’s important to understand these fundamentals before moving on to bigger and badder projects. Please see the links below if you would like to go more in depth with this topic. Cheers

--

--

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