4 basic principles of object-oriented programming in JavaScript

Object-oriented programming is a programming style that focuses on objects, not functions. Let's take a simple example of a car that is an object with properties such as model, color, and methods such as starting the vehicle, stopping, and driving. There are four basic principles of object-oriented programming. Without them, a programming language cannot be called object-oriented. These principles are encapsulation, inheritance, polymorphism, and abstraction. Learn about these principles, what they mean and how to apply them in this article.


Hermetization

The first of the basic principles of object-oriented programming is encapsulation. It consists in separating the internal and external interface of our object, so that certain methods and properties are hidden from the external environment.



Suppose we have a machine that produces motors. Such an object will have many properties and methods that will work inside it. As a programmer, you don't want others to be able to change every member of such an object, because that might stop working. To implement encapsulation in JavaScript, we create a new class. Inside, we declare new properties. All of them are private and will keep them hidden and will not be accessible from outside the classroom. From now on, the only way to access them is through the methods inside this class.



Inheritance 

Another of the most common principles of object-oriented programming is inheritance. Inheritance helps you extract common attributes and avoid writing the same code. Instead, you can let other classes "inherit" from this separate class. In this case, the class inherited from other classes is called "parent class" and classes that inherit from this "parent class" are called "child classes". Inherits all parent properties and methods. The only exception is private properties and methods.



Polymorphism

The third principle of object-oriented programming is Polymorphism. Suppose you have several classes related to each other by inheritance. Parent class and child classes. Two things must happen for polymorphism to occur. First, one of these classes creates its own method. Second, this method somehow overrides the method with the same name that is declared in the heir class. If you've learned to drive one car, you'll be able to drive any other car, no matter what brand or its configuration, it works in a similar way. Same in development, if your code can work with one interface implementation, it should work with another as well.



Abstraction

The last principle of programming is abstraction. The idea behind this principle is that the outside world should only receive relevant information for use. It should not contain information about the implementation details of this object. Abstraction helps us reduce code duplication, swap the implementation without breaking anything, and the implementation in one place, which allows us to easily respond to breakthrough changes.



Summary

After reading this article, you should know what encapsulation, inheritance, polymorphism, and abstraction are all about. Let me know what you think and share the article if it helped you.