Object-Oriented Programing and Benefit

CG_Musta
3 min readJun 25, 2020

The 4 Pillars

Encapsulation
Abstraction
Inheritance
Polymorphism

What we had before the OOP??

Before OOP, there was procedural programming. That divided the programs into a set of functions, so the data were stored in many variables and functions that operate under data. The problem is, while the program grows, we end up with a lot of functions in our program, and we have to copy and past code over and over again. To make a change to one function and then to the other functions as well.

How OOP solved the problem?

OOP combine a group of related variables and functions into a unit, and that unit is called Object.
The variable will be the property of the Object, and the function of the method.

Encapsulation

Car Example

The car properties are the make, model and colour. The methods are start(), stop() and move().
In Object-oriented programming, we group this method and properties, and we called them Encapsulation.

Code Example

We created a function greetPeople() which take three arguments and create a greeting phrase.
Now let see how we can solve the same problem using OOP.

We have an object with three properties and a method called greetPeople with no argument.
The method does not have any parameters because all the three parameters are modelled as properties of the Object.
Now all these three properties and the methods are part of one unit. One of the disadvantages of using normal functions is that there are many parameters while in OOP, the functions or methods have fewer parameters. The fewer is the number of parameters, the easier it is to use and maintain that function.

Abstraction

Think about tv as an Object, from the inside has some complex board and from the outside has a button that you can interact with. When you use the tv you simple press the button, and you don care about what’s happening inside. This is the concept of abstraction in practice, in our Object, we can hide properties and methods from the outside, and these give us many benefits such as:

Simplify the interface

By giving just a few methods to interact with from the outside, instead of many methods and properties which can create complexity in the use of the Object.

Reduce the impact of Change

If you decide to modify or change some of the property or methods, this will not compromise other code in the project.

Inheritance

Some HTML elements like TextBox, Select and checkbox. All this elements has some properties in common (hidden, innerHTML…) and method like click() and focus(). Instead, to redefine these properties and method for each HTML elements, we can define them once in an Object called HTML Elements and have other objects inherit these properties and method, and this will help us to eliminate redundant code

Polymorphism

Poly = Many

Morph = Form

These means many forms and help you to get rid off some If, else or lengthy switch case statement. Because every element needs some sort of logic to render it on a page in a different way, in OOP, we can implement the render() methods differently in every single Object, and this will behave differently depending on the type of Object we are referencing.

Sum up all the benefit of the OOP.

Encapsulation: we group related variable and functions together reducing complexity, and we can re-use this Object in a different part of the program or different programs

Abstraction: we show just the essential part we need to use, and we hide details and complexity, and this will reduce complexity and isolates the impact of the changes in the code

Inheritance: we eliminate redundant code

Polymorphism: we can refactor complex lengthy and unnecessary statements like switch case and if-else.

--

--