Object-oriented programming (OOP) is a computer programming model that manages software design around data or objects. In OOP or object-oriented programming, there are four core concepts, these concepts
are encapsulation, abstraction, inheritance, and polymorphism.
Let us look at each of these concepts in detail.
Encapsulation
Encapsulation is the grouping of related variables and functions into objects. An example of encapsulation in action.
Suppose we have three variables, base salary, overtime, and
rate. We also have a function to calculate the wage for an employee. We can
couple together all properties and methods into an object which
could be named an employee object.
In
an object-oriented way, your functions end up having fewer and fewer parameters as
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.
Abstraction
Abstraction
in object-oriented programming is the technique of hiding unnecessary details
from users. Let us take an example of a TV as an object,
this TV has a complex logic board inside and a few buttons outside that you
interact with. Users simply press the buttons
to operate the TV and don't care about what happens inside. All that complexity is
hidden from users, this is Abstraction in practice.
We can use the same technique in our objects. We can hide some of
the properties and methods from the outside; this gives us a couple of
benefits. First is that we will make the interface of those objects
simpler. Using 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 us imagine that tomorrow we change these inner or private
methods, these changes will leak to the outside because we don't have any code
that touches these methods outside of their containing object or class. We may delete a
method or change its parameters but none of these changes will impact the
rest of the application's code. With abstraction, the impact of the change could be
reduced.
Inheritance
Inheritance
in OOP is a mechanism that allows you to eliminate redundant code. Here is an
example, think of HTML elements like text boxes, drop-down lists, checkboxes, and so on. All these elements have a few things in common they
should have properties like hidden and innerHTML 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 this generic
element an HTML element and have other objects inherit these properties
and methods. Inheritance helps us eliminate redundant code.
Polymorphism
In polymorphism, poly means many; mor means form so polymorphism means many forms. In object-oriented programming, polymorphism is a technique that allows you to get rid of long if-else or switch and case statements.
In the previous example, we talked about HTML elements. All those objects should have the ability to be rendered on a page but the way each element is rendered is different from the others. If you want to render multiple HTML elements by using object orientation we can implement a render method in each of those objects with different behavior depending on the type of the object you're referencing. We can get rid of this nasty switch and case and use one line of code.