OOP, or object-oriented programming, is a paradigm for programming that is built on the idea of objects;
Using objects, we can represent concrete or abstract qualities;
In OOP, objects are self-contained pieces/blocks of code. OOP was developed to organize code, to make it more flexible and easier to maintain(avoid “spaghetti codeâ€).
To create the new objects programmatically we use classes for that. You can think of a class is like a blueprint that can be then used to create a new object based on a rule, described in the class. So it’s just like a blueprint from which we can create multiple new objects. For e.g from a single blueprint, we can create multiple houses.
To design classes, there is not a single best technique for designing classes. There are however four fundamental principles that can guide us towards a good class implementation, and these principles are:-
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
1. Abstraction:- Ignoring or hiding details that don’t matter, allowing us to get an overview perspective of the thing we are implementing, instead of messing with details that don’t matter to our implementation.
Abstraction is really important not just only in OOP but in programming in general. We create and use abstraction all the time.
2. Encapsulation:- Is the practice of keeping methods and properties private inside the class, so they are not accessible from outer classes. Some methods can have a public interface (API).
With this critical property, we prevent external code from accidentally manipulating internal properties/state(Object Data) which can reduce the chances of bugs, especially for large code. To make it encapsulated we use private before property that will not be accessible from outside but still accessible from within the class.
3. Inheritance:- Establishing a hierarchical relationship between classes by making all of a class's properties and methods accessible to its child class. This enables us to reuse common logic and model relationships seen in the actual world.
4. Polymorphism:- A child can overwrite a method initiated from a parent class.
JavaScript is not a class-based object-oriented language. However, it still uses some sorts of object-oriented programming (OOP).
In JavaScript, we have something called Prototype. In JavaScript, each object is connected to a particular prototype object. This means each object has a prototype.
The prototype contains methods (behavior) that are accessible to all objects linked to that prototype, this is called prototypal inheritance.