OOP Concepts - Pillars of Object Oriented Programming

Using objects in programming improves code maintainability, productivity, flexibility and re usability and help building a secure program
OOP concepts is an important part of software development. You can't become a programmer, a web developer or a mobile developer unless you understand the object-oriented approach.



Object-oriented Programming, OOPs is a technique of programming that uses objects in programming.

It binds data and the functions that operate on the data into a single unit called object.

The data is accessed to only functions or methods that belong to the object.

This way it ensures that outside code can't access or alter the data and thus, securing the program data from inadvertent changes.

In the typical procedural programming approach, it was difficult to protect the data from such inadvertent changes. The data can be easily modified by any function. And that can affect the functionality of other functions that use the same data.

In Object-oriented Programming, the object does its operation without the caller having to worry how it actually does it.

Our Software objects are just like our real world objects such as car, cycle, dog etc. They all have state and behavior. For example, a Car has state like Color, Brand, Weight, Model etc. and behavior  as braking, accelerating, slowing down etc. Software objects too, have state and behaviors. They maintains its state in variables and implements its behavior or function with methods.

Using objects in programming improves code maintainability, productivity, flexibility and re usability and help building a secure program.

There are 4 major principles or building blocks of Object Oriented Programming. They are also called as four pillars of Object Oriented Programming.

And they are Encapsulation, Abstraction, Inheritance and Polymorphism

So now let's discuss each of them

Encapsulation

In simple language, Encapsulation is nothing but a process of putting something into a capsule. In programming, it is the process of binding logically related data and operations together in an entity called class. By doing so, it actually improves readability and maintainability of the program.

Using Encapsulation, we can also achieve information hiding or data hiding. Let's try to understand this from real life context. We live in a house and it has many items. Now, different people visiting our house have different access rules to these items. Those who are staying in the house might have full access and friends and relatives will have limited access.  

Similarly in programming, Encapsulation provides a protective layer that prevents the data from being accessed by the code outside this layer.

Thus, Encapsulation prevent corruption or modification of data by other entities.

And this is done through access control modifiers (i.e. public, private, protected).

Abstraction

Abstraction is hiding the complexity of the program. It is the process of exposing the essential details of an entity, while hiding the irrelevant details. Like in a car, if you want to apply brake, you don't need to know how the brake mechanism works, you just need to know which one is brake padel. Here the knowledge you have for your car is Abstract. Similarly, in software programming, when you want to perform a task, you will try to find out a ready method that can do the job for you. And If you get a ready method, you don't need to know how it is implemented, you can just invoke it. So, using Abstraction principle in programming, you actually hide details and complexity of reusable code and show only essentials details. Just the way we use TV at home without knowing about its internal working. We simply operate them and internal details are abstracted from us. This is abstraction.

Inheritance

Inheritance refers re-usability of the code where new object can inherit the property of parent object, thereby establishing a parent-child relationship. Using Inheritance, the new object can enjoy the existing functionality of parent and can also add new features and functionalities as per requirement.

For example,  there are different types of calculators such as Basic Calculator, Scientific Calculator, Online Calculator etc. All these are extend forms of Calculator. Here, Calculator is parent and Basic Calculator, Scientific Calculator and Online Calculator are Child objects.

Inheritance allows you to create class hierarchies, where a base class gives its behavior and attributes to a derived class.

Polymorphism

The word "poly" means many and "morphs" means forms. So, The word polymorphism means having many forms.

Let's understand polymorphism using real world example.

A person at the same time can speak more than one language. So, he can display different behavior in different situations. so we can say person object is polymorphic in nature.

Another real world example is your mobile phone. Your mobile can behave like a phone, camera and sometime as a radio. Here, the same mobile phone has different forms, so we can say the mobile object is polymorphic in nature.

In object-oriented programming, polymorphism refers to the ability of a variable, function an object to take on many forms.

The goal of Polymorphism in Object-oriented programming is to enforce simplicity, improve maintainability and make code more extendable.