And I OOP (Object-oriented Programming)
Tags: webdev, javascript, programming, oop
Published: 2024-04-13 22:43:34

Hi there! Welcome back to my blog!

If you have been following along you already know the drill, but if you are new, hi! My name is Paul and I am a former student with [Flatiron School](www.flatironschool.com) who is on a journey of discovery and.. job hunting. 

As the title suggests we will be talking about **OOP** or **object-oriented programming**. What is OOP? How can we use it? Is it useful? To find the answers to these questions, read along.

_Disclaimer_

I wanted to give a node to the 2019 Meme winner 'And I oop' [Jasmine Masters](https://knowyourmeme.com/memes/jasmine-masters-and-i-oop).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6a9t032vljcm49vzrv5i.png)[Source](https://helios-i.mashable.com/imagery/articles/02tTwwEQQPKxmBpItB2YgX4/hero-image.fill.size_1200x675.v1614271686.jpg)

## What is OOP?
Object-oriented programming aka OOP is a fundamental programming model for other programming languages such as C++ and Java. 
The OOP is divided in 3 strong concepts: **classes and instances**, **inheritance**, and **encapsulation**. 

These concepts are pretty familiar with other subjects we've tackled on this blog such as [Ruby](https://dev.to/paulxcodes/shine-bright-like-a-ruby-25d2). The model behind object-oriented programming is a collection of objects. Each object representing a particular aspect of a system. Objects are a combination of **functions** (or as we remember them from Ruby: _methods_) and data. Because the object maintains its own private internal state, the system doesn't have to care what might happen inside the object. 

## Classes and instances
When we think about the types of objects we want to include in our system, we create abstract definitions. For example, if you are modeling a university, the professors will represent some objects. Every object is represented by some properties in common: having a name and teaching a subject. Furthermore, a professor can grade papers/projects and can introduce themselves at the beginning of every year. 

That would make the **Professor** a **class** in our system. The pseudocode would look like this: 
`class Professor
   properties 
     name
     teaches 
   methods
     grade(paper)
     introduceSelf()
` 
[Source](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming)

We defined the **Professor** class with 2 _properties_ (name and teaches) and 2 _methods_( **grade()** to grade papers and **introduceSelf()** to introduce themselves)

Solely by itself, a class won't do much. It will act as a blueprint for creating other objects from the same type. Each professor is created is an **instance** of the **Professor** class. The function responsible for creating instances it's called **constructor**. The values passed to the constructor represent the internal state we want to initialize in the new instance. Example: 

`class Professor
   properties 
     name
     teaches
   constructor
     Professor(name, teaches)
   methods
     grade(paper)
     introduceSelf()
`
[Source](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming)

With the constructor in place, let's create some professors!
P.S programming languages mark the signal for calling a constructor with **new**.

```

stephenie = new Professor("Stephenie", "Biology")
adam = new Professor("Adam", "History")

stephenie.teaches; // 'Biology'
stephenie.introduceSelf(); //'My name is prof. Stephenie and I will be your Biology professor.' 

adam.teaches; // 'History'
adam.introduceSelf(); //'My name is prof. Adam and I will be your History professor.' 
```
[Source](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming)
 The command above creates 2 new objects representing instances of the **Professor** class! How fun is that?!

## Inheritance
Who else goes to university? Oh yes! Students. What separates students from professors, is that they can't grade _(officially)_ papers, they don't teach any particular subject and they are usually part of a graduation year. 
But, there are some aspects that are common. They also have a name and want to introduce themselves. So the class would look something like this: 
`class Student
   properties 
      name
      year
   constructor
      Student(name, year) //to be able to create instances
   methods
      introduceSelf()
`

Now we can notice that we are repeating ourselves and as respected software engineers we have to keep it D-R-Y! (Don't Repeat Yourself). If only we could find a way to write a code that represents both students and professors on the same kind of level.. Hmm. What about creating a new class called Person where we set the common properties:

`class Person
    properties 
      name
    constructor
      Person(name)
    methods
      introduceSelf()

class Professor: extends Person 
    properties
      teaches
    constructor
      Professor(name, teaches)
    methods 
      grade(paper)
      introduceSelf()

class Student : extends Person
     properties 
       year
     constructor 
       Student(name, year)
     methods
       introduceSelf()`


The **Person** became a _superclass_ or _parent class_ for both **Professor** and **Student**. The same way around, **Student** and **Professor** are _subclasses_ or _child classes_ of **Person**. 

The introduceSelf() method is repeating itself in all classes as we need to differentiate the type. For example 

```
susan = new Professor("Susan", "Drama")
susan.introduceSelf(); // 'My name is prof. Susan and I will be your Drama professor.'

john = new Student("John",2)
john.introduceSelf(); // "My name is John and I'm in my 2nd year.'

dan = new Person("Dan")
dan.introduceSelf(); // "My name is Dan."
```
[Source](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming)

## Encapsulation
 Due to its private internal state, the object's internal methods cannot be accessed by other objects. Keeping the internal state of an object private and the clear division from the public interface and private state is called **encapsulation**. 

Let's say the students can study a certain subject only in their 2rd year or above, how can we make sure we have a system that checks the student's year?

We can pass a method in the Student objects with the logic: 
`class Student : extends Person
    properties 
       year
    constructor
       Student(name, year)
    methods
       introduceSelf()
       canStudyLiterature() {return this.year > 1 }
`

```
if (student.canStudyLiterature()){
  //allow the student into the class
```
[Source](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming)

**Tip!**
To be sure other code can't access the object's internal state, we can use the property called **private**. This will throw an _error_ if some code outside the object tries to access it. 

`class Student : extends Person 
    properties 
       private year
    constructor
       Student(name, year)
    methods
       introduceSelf()
       canStudyLiterature() {return this.year > 1 }

student = new Student('Dana', 1)
student.year // error: 'year' is a private property of Student
`

How fun is that?! 

## Summary

In this blog, we went over the basics of object-oriented programming. I hope you enjoyed this and found some parts useful. It's a learning experience for us all and if you want to learn more go ahead to [mdn web docs](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_programming) and get more details. 

Till next time, happy coding!