This is a continuation of the post from last week on the pillars of OOP.
The four pillars are the most important concepts behind object-oriented programming. Here are short explanations of the final two; encapsulation and polymorphism.
Encapsulation
This is the principle of hiding internal object data and providing the ability to update the data through exposed methods. In JavaScript of course, there is no way to declare something to be private. Instead, we encourage users to interact with an object's data via the methods that we provide through api documentation.
Using methods instead of updating data directly allows us to do things like check that the user is providing valid data.
Polymorphism
This is the idea that different objects can have the same interface but still provide functionality unique to the individual object. A commonly used example of this is shapes. You can have a circle object and a square object, both with draw() methods. The implementation will be different, but the interface is the same: the user calls Square.draw() or Circle.draw().
The concept of polymorphism also relates to inheritance because the idea is that you can overwrite general behaviors (methods) from the parent object with specific behaviors in the child. Recall the animal inheritance example from the previous article. To illustrate polymorphism, we can add a makeSound method to Animal and then overwrite it in the child objects, while still keeping the same interface.
You may see other types of polymorphism in different languages, like Java's 'static polymorphism.' This is just a basic overview of the concept, but just know that it goes much deeper than what I wrote here.
Conclusion
I hope you enjoyed this article. Please let me know your thoughts in the comments below.
This series will be continued next week with an article on the basics of functional programming. See you then.