Object-oriented Programming in 7 minutes

What are the four pillar concepts of OOP

The four pillar principles of object-oriented programming are

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

In procedural programming, programs are divided into a block of code called functions. Procedural programming could be simple and straightforward. However, for complicated software, programmes written with just procedural programming could become very complicated and unreadable - Spaghetti code.

OOP to the rescue

OOP groups related variable and functions into units called objects. The variables in an object are referred to as properties and the functions as methods. OOP is inspired by real-world objects like cars and pieces of furniture. Ex. A car has a property like ' the number of tyre' number and a method like 'start and move'.

In your chrome console, you can see objects like local storage

Encapsulation

Encapsulation describes the idea of building data and methods within a unit of an object. The data and methods are combined into a single unit called class

Example:

let base_salary = 30,000;
let overtime = 100;
let rate = 2;
function getWage(base_salary, overtime, rate) {
 return base_salary + (overtime * rate);
}

This is a typical procedural program, with OOP an instance of Employee class will be created with a value for each propery

Example:

let employee = {
    base_salary: 30,000.00;
    overtime: 100;
    rate: 2;
    function getWage() {
     return this.base_salary + (this.overtime * this.rate);
    }
}

Subscribe for coding videos

Keep growing professionally with just three curated videos weekly.

We hate spam as much as you do.