You down with OOP? Yeah you know me!
Tags: todayilearned, programming, beginners
Published: 2022-02-25 04:21:37

## A Beginner's Attempt at Explaining Object Oriented Programming

Wikipedia is great, but when I attempted to look up Object Oriented Programming I was confused after a single sentence:

> Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code...

Actually, I made it five words in before I had questions if I'm being honest with myself. What's a "programming paradigm"? Once again, Wikipedia to the rescue:

> Programming paradigms are a way to classify programming languages based on their features.

Ok, so OOP is a collection of features that leverages the concept of objects. In this post, I'll attempt to explore what an object is along with the main features of OOP.


## What's An Object?

The second half of Wikipedia's definition alludes to the idea that objects are made up of data and code. Generally speaking, objects are a collection of data in the form of properties/attributes and code in the form of procedures/methods. 

In other words, an object is made up of code that allows the object to do things along with data that gives the object some kind of state.

Think of a motorcycle as an example. If the motorcycle is the object, there might be a method to turn the engine on, speed up, slow down, and turn off. Additionally, there may be data to tell us what color it is, how fast it's going, etc. Ultimately, the object "motorcycle" is a collection of these properties and methods. 


## Where Do Objects Come From?

An important idea in OOP is the relationship between objects and classes. A class acts like a template for the data and methods associated with the objects the class creates. If our motorcycle is the object, the Harley factory could be the class. The factory stores all the information needed to create a new motorcycle. 


## What Are the Main Features of OOP?

The dynamic between classes and objects allows for the implementation of features that are advantageous when engineering software. 

As part of its programming paradigm, OOP has four main features: Encapsulation, Abstraction, Inheritance, and Polymorphism.

**Encapsulation**

At this point I just start thinking of things in terms of boxes. The class is a giant box that has a bunch of info regarding how to create objects. Each object the class creates is also its own box. Within each of these classes and object containers is info about that container's state and how it can behave. 

Encapsulation is the idea that the information in these containers can be either public or private. The ability to define those privileges gives us the power to define how objects interact with one another. 

**Abstraction**

The bigger a program gets, the more difficult it can be to manage. Let's say it's first thing in the morning and you want/need a cup of coffee. Would you rather build a coffee maker from a pile of parts, or push a button and get on with your day? 

Abstraction is the process of stripping away the complex inner workings of a particular feature and exposing only the critical details of an object to its client. The class/object relationship creates a dynamic that allows abstraction to occur. 

**Inheritance**

Often times we have objects that are similar, but not identical. The class/object relationship allows for the creation of objects that vary slightly from one another, but creating those objects would be difficult if done individually. Doing so would require lots of repetitive code. Enter inheritance: the ability for objects to inherit code from other objects. 

Let's say we want to build a bunch cars, trucks, and busses. We could build each one from scratch, but that would require a lot of repetition. Instead, we could start with a parent class that has all the info shared by each vehicle (e.g., 4 tires, an engine that turns on and off, brakes, etc.). We can then create more specific classes that _inherit_ all that info from the parent class, but also contain their own specific properties and methods. Using this method, both a sports car and a minivan could _inherit_ the same code from a parent class, eliminating the need to rewrite all that code.

**Polymorphism**

Polymorphism is the ability to exist in different forms. In the world of OOP, it refers to a dynamic that allows for flexibility with how methods are executed. 

It's like filling a glass with water from a pitcher. The water itself doesn't change when you pour it into a glass, but the water is flexible enough to adapt to its new container. 

We demonstrate polymorphism by writing code that is flexible in its implementation. For example, a motorcycle might inherit a method for turning on, but that method could be implemented in different ways (e.g., push start, key turn, kick start, etc.). 


![Bruce Lee water quote](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21gvg0ahv996gm2wir1u.jpeg)

## Sources

- [Wikipedia: Object-oriented programming](https://simple.wikipedia.org/wiki/Object-oriented_programming)
- [How to Explain Object-Oriented Programming to Kids](https://funtech.co.uk/latest/explain-object-oriented-programming-to-kids)
- [How to explain object-oriented programming concepts to a 6-year-old](https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/)
- [The Four Basic Concepts of OOP](https://www.indeed.com/career-advice/career-development/what-is-object-oriented-programming)
- [Top 50 OOP Interview Questions & Answers](https://dailyjobopening.in/2021/11/02/top-50-oops-object-oriented-programming-interview-questions-answers/)