A popular interview question concerns the four core concepts in object-oriented programming. These concepts are encapsulation, abstraction, inheritance, and polymorphism. Let's look at each of these concepts. Before object-oriented programming, we had procedural programming that divided a program into a set of functions.
So we have data stored in a bunch of variables and functions that operate on the data. This style of programming is very simple and straightforward. Often it's what you learn as part of your first programming subject at a university. But as your programs grow, you will end up with a bunch of functions that are all over the place. You might find yourself copying and pasting lines of code over and over.
You make a change to one function and then several other functions break. That's what we call spaghetti code. There is so much interdependency between all these functions it becomes problematic object-oriented programming came to solve this problem in object-oriented programming we combine a group of related variables and functions into a unit we call that unit an object we refer to these variables as properties and the functions as methods here is an example think of a car a car is an object with properties such as make model and color and methods like start stop and and move.
Now you might say, but Mosh, we don't have cars in our programs. Give me a real programming example. Okay, think of the local storage object in your browsers. Every browser has a local storage object that allows you to store data locally.
This local storage object has a property like length, which returns the number of objects in the storage and methods like set item and remove item. So in object oriented programming, we group related variables and functions that operate on them into objects. And this is what we call encapsulation.
Let me show you an example of this in action. So here we have three variables, base salary, overtime, and rate. Below these, we have a function to calculate the wage for an employee. We refer to this kind of implementation as procedural. So we have variables on one side and functions on the other side.
They are decoupled. Now let's take a look at the object-oriented way to solve this problem. We can have an employee object with three properties, base salary, overtime and rate, and a method called getWage.
Now why is this better? Well, first of all, look at the getWage function. This function has no parameters.
In contrast... In a procedural example, our getWage function has three parameters. The reason in this implementation we don't have any parameters is because all these parameters are actually modeled as properties of this object. All these properties and the getWage function, they're highly related, so they're part of one unit.
So one of the symptoms of procedural code is functions with so many parameters. When you write code in object-oriented way, your functions end up having fewer and fewer parameters. As Uncle Bob says, the best functions are those with no parameters.
The fewer the number of parameters, the easier it is to use and maintain that function. So that's encapsulation. Now let's look at abstraction.
Think of a DVD player as an object. This DVD player has a complex logic board on the inside and a few buttons on the outside that you interact with. You simply press the play button and you don't care what happens on the inside.
complexity is hidden from you. This is abstraction in practice. We can use the same technique in our objects. So we can hide some of the properties and methods from the outside and this gives us a couple of benefits. First is that we'll make sure that we have a good amount of time to do the work.
the interface of those objects simpler. Using and understanding an object with a few properties and methods is easier than an object with several properties and methods. The second benefit is that it helps us reduce the impact of change. Let's imagine that tomorrow we change these inner or private methods. None of these changes will leak to the outside because we don't have any code that touches these methods outside of their containing object.
We may delete method or change its parameters but none of these changes will impact the rest of the applications code so with abstraction we reduce the impact of change now the third core concept in object-oriented programming inheritance inheritance is a mechanism that allows you to eliminate redundant code here's an example think of HTML elements like text boxes drop-down lists check boxes and so on all these elements have a few things in common. They should have properties like hidden and inner HTML and methods like click and focus. Instead of redefining all these properties and methods for every type of HTML element, we can define them once in a generic object, call it HTML element, and have other objects inherit these properties and methods.
So inheritance helps us eliminate redundant code. And finally, polymorphism. Poly means many, more, and more. Orph means form. So polymorphism means many forms.
In object-oriented programming, polymorphism is a technique that allows you to get rid of long if-and-elves or switch-and-case statements. So back to our HTML elements example, all these objects should have the ability to be rendered on a page. But the way each element is rendered is different from the others.
If we want to render multiple HTML elements in a procedural way, our code would be probably look like this. But with object orientation, we can implement a render method in each of these objects, and the render method will behave differently depending on the type of the object we are referencing. So we can get rid of this nasty switch and case and use one line of code like this.
You will see that later in the course. So here are the benefits of object-oriented programming. Using encapsulation, we group related variables and functions together. And this way we can reduce complexity. Now we can reuse these objects in different parts of a program or in different programs.
With abstraction, we hide the details and the complexity and show only the essentials. This is how we do it. technique reduces complexity and also isolates the impact of changes in the code. With inheritance we can eliminate redundant code and with polymorphism we can refactor ugly switch case statements. Well hello, it's me Masha again.
I wanted to say thank you very much for watching this tutorial to the end. I hope you learned a lot. Please share and like this video to support me.
If you want to learn more about object oriented programming as I told you before, I have a course called Object Oriented Programming in JavaScript. If you want to learn more, click on the link in the video description and enroll in the course. And if not, that's perfectly fine. Make sure to subscribe to my channel because I upload new videos every week. Thank you and have a great day.