Fundamentals of Class-based OOP
Tags: beginners, programming, learning
Published: 2024-03-13 11:20:38

There are four fundamental concepts of Object-Oriented Programming:

1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism

Before we dive into these concepts. First:

## What is an Object?
You can **think of an object as a noun**. Such as a car, book, place or even bank account.
In terms of programming, an Object is anything that you want to **store and process data about**. _Another name for an object is an entity_.

Still a bit complicated to understand? 

Let's make it simpler.

**Think of an object as if it were a Lego block**. It has its own _color, shape, size and pain level_, when stepping on it.
**You use these blocks to build a Lego house.**

The _house (software program)_ is made up of different _blocks (objects)_.
Do you understand? If not, take a hard look at yourself and if programming is for you in the first place.

## 1. Abstraction
Abstraction is like simplifying reality. 

Hear me out. A person is an object, but if we were designing a new program to process data about a person, we would only concern ourselves with the relevant data of the person and how we intend to use this data.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbv18cwuycpcwk24dbpx.png)

To create objects programmatically, we need a **Class**.

#### Class
A class is a template for creating objects.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6nbiasexc0etdh9t6ct.png)

A class is often compared with a pastry cutter. Because once it's created, it can be used to create many objects of the same type.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9vrplc9m7uigwuqbmirr.png)

Each object is an instance of this class.
`Person p4 = new Person();`
The above creation of the p4 object is called instantiation.


## 2. Encapsulation
Encapsulation is hiding data and complexity.

Think of a treasure chest. It holds valuable items inside, and it's locked to keep those items safe. In programming, a class acts like a treasure chest. 
It holds data (the treasures) and methods (the locks) to access or manipulate that data. The data inside the class is hidden from outside access, just like the treasures are hidden inside the chest.

Encapsulation helps keep the complexity of a program under control by organizing related data and functions together. 

## 3. Inheritance
Inheritance is exactly what it says. A class can derive it's methods and properties from another class. Just like a child inherits from a parent.

For example, a `class Employee` can derive from the `class Person` because an Employee is a Person. 
The same can be same for a `class Customer`, because a customer is also a Person.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlxhmioueyr08gd5euzo.png)

Classes that inherit from other classes can include their own properties/data. They can extend from the class they are inheriting.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rwuo9365x0doq8y9oykb.png)

## Polymorphism
This is when a class implements an inherited method in it's own way.
The subclass(child) can override any method or property from the superclass(parent).

[Learning resource credit](https://www.youtube.com/watch?v=m_MQYyJpIjg)