OOP Basics
Tags: computerscience
Published: 2021-09-27 10:17:57

I'm going to go over object-oriented-programming("OOP") so that I can review it whenever I need to. 

**What was the problem before OOP?**
First, let's go over what kind of problem it solved. Before, OOP the dominant programming paradigm was procedural programming. Procedural programming was simply just a lot of data stored in variables and a functions that performed some code on those variables. The problem was that as this type of code scaled the same lines of code were being used repeatedly throughout the project (aka spaghetti code). 

**How did OOP solve this problem?**
*Encapsulation*
What OOP did was simply organize related variables and functions together into objects. The variables become known as properties and the functions became known as methods.

```js
//Procedural programming
let firstName = 'Joseph';
let lastName = 'Kim';

function findFullName(firstName, lastName) {
  return firstName + lastName;
}

//OOP
let user = {
  firstName: 'Joseph',
  lastName: 'Kim'
  findFullName() {
    return this.firstName + this.lastName
  }
}
user.findFullName();
```

As we can see in the example above, in procedural programming the variables and functions are decoupled while in OOP they are organized into one object. In what way is this better? There are less parameters in the OOP style. With fewer parameters it is easier to maintain the function and considered better practice in the coding world.

*Abstraction*
Abstraction just means it hides the complexities away from you so that you don't have to deal with it. For example, think of a microwave. You just have to click start for the microwave to start. You do not have to spend time trying to understand how the microwave works internally in order to use it. This concept is basically the same in programming (e.g. you don't have to know how libraries work internally to use the methods that it provides).

What benefit does this have?
1)Objects can have many properties and methods but some of them can be hidden from the outside making it simpler to work with. 2)Another benefit is that it reduces the impact when changes are made. If methods or properties are hidden or kept private then when a problem occurs we know that those properties or methods are not part of the problem that occurred. 

*Inheritance*
Inheritance is when you inherit some properties or methods from another object or class. This helps because you don't have to write the same method or property each time you need it in different objects. You can just make it once and reuse it.

*Polymorphism*
This is a really weird word but it simply means many(poly) + forms(morphism). So how is this relevant to programming? Long story short, it helps us not use endless amounts of if/else statements. How is this possible? Let's use an example. There are three classes: Animal, Pig, and Dog. Dog and Cat are child objects of Animal which means it can access Animal's properties and methods. 

```typescript
//Dog extends Animal
//Pig extends Animal

let kingdom: Animal[] = new Animal[];
let doggy = new Dog();
let piggy = new Pig();
kingdom.push(doggy, piggy);

for (let animal of kingdom) {
  animal.eat(); 
  animal.sleep();
  //we don't need to use if dog then sleep or if pig then sleep
  //we can just use eat() or sleep() because they are child object of Animal;
}
```