OOPs Pillars

OOPs Pillars

OOPs series in C++

·

4 min read

Abstraction:

It is a process of showing concerned information and hiding unimportant and complex details. Abstraction In Real Life: Abstraction is present almost everywhere. Let’s understand it by some examples.

  • When we are taking a selfie, we are only concerned with how to take selfies like which button is used, how to zoom in/out, etc. We are not concerned about how the actual mechanism works internally. We are just not interested in those details. It means that we are just dealing with the “abstract” view of these operations that will help us to take the selfie.
  • Another example is, the only thing that we need to know in order to be able to use a coffee machine is that we put inside the coffee, water and press a button and finally we got a hot cup of coffee but the process internally is very complex. We do not need to know how internal components of that coffee machine work together in order to prepare that coffee. That process is actually abstracted from us. Because it is not important for the user.

Advantages Of Abstraction:

It has a lot of advantages in the sense that, It Increases the security of an application by putting a barrier against change propagation and also reduces the complexity level as the programmer hides the implementation details from the user and exposes only the functionality to the end-user. So, the user will only know “what it does” rather than “how it does”.

Encapsulation:

In dictionary terms, encapsulation means the action of enclosing something in or as if in a capsule. In a programming language, it is defined as: “Wrapping of data, along with the methods into a single unit is called encapsulation.” Encapsulation is also called “data hiding”.

More About Encapsulation:

Encapsulation may also refer to a protective shield that prevents the data from being accessed by the code outside this shield. In order to get access to that data and want to perform some actions, we must define a method associated with the object which does this. Then whenever we want to perform this action we call that specific method on the object. In oops(object-oriented programming language) we get the benefit of encapsulation by making classes.

Encapsulation In Real Life:

Here is an example to get a clear idea.

  • Suppose you have an account in the company’s bank. If your password variable is declared as a public variable in the company software, your pin code will be known as public, In this case, anyone can easily withdraw money. So, would you like it? Obviously No. So, they declare password variable as private for making your account safe, so that anyone cannot see and access your account.

Advantages Of Encapsulation:

  • Hide Data: Using private access specifier.
  • Low complexity: As users have no idea about class implementation.

Inheritance:

In simpler terms, “The reception of genetic qualities by transmission from parent to offspring is called inheritance.”

In programming term, “ Inheritance is a mechanism in which one class get the properties of another class.”

These properties are class methods and data members. We will learn about methods in upcoming blogs. A class whose properties are inherited is called base or parent class. The class that inherits the properties from the parent class is called the Child class. But the important point is “ Child class can have its own attributes and methods.” Is it interesting? Yes :)

Inheritance In Real Life:

  • Stationary is a broad category. Ball pens, pencils, pages, colors, etc all inherit properties from stationary class i.e all are used for writing.
  • Android is a parent class, all android models like J5 prime, oppo J2, etc are the child classes, that inherit properties from the base class.
  • Inheritance is the most important term used in the field of genetics.

Advantages Of Inheritance:

  • Code Reusability
  • Saves time: Because we do not need to write the same code for the child class.

Polymorphism:

Polymorphism is a combination of two Greek words, “Poly” means “many” and “Morphism” means “forms”. Ability of an object to be present in more than one form.” Polymorphism can also be referred to as a “ Function with the same name in two classes but have different actions.

Polymorphism In Real Life:

  • Suppose a person is a teacher in a school. He will be a teacher in school, but when he is in a shopping mall, he’ll be a customer, in a bus he’ll be a passenger, at home he’ll be a son/husband. This means to say that there is one person but has different roles and functions at different locations.

Advantages Of Polymorphism:

  • Reusability
  • Allow the object which form of function is implemented I know, beginners do not understand it very well. But don't worry. We’ll look at this topic in detail using code.

Conclusion:

There are four pillars of object-oriented programming. Abstraction, Encapsulation, Inheritance, and Polymorphism. Abstraction is hiding unnecessary details. Encapsulation is hiding private data from the outside world. Inheritance gives the benefit of reusability. Polymorphism is one name, many forms. Hope you like these simple concepts. Happy Learning :)