CS Electrical And Electronics
@cselectricalandelectronics
All PostsCPPProgrammingWhat Is

What Is Inheritance, Polymorphism, Abstraction, Encapsulation In OOPs

Hello guys, welcome back to our blog. Here in this article, we will be discussing what is inheritance, polymorphism, abstraction, and encapsulation in OOPs, when these OOPs concepts are utilized, and with examples, we will be explaining.

If you have any electrical, electronics, and computer science doubts, then ask questions. You can also catch me on Instagram – CS Electrical & Electronics.

Also, read:

What Is Inheritance, Polymorphism, Abstraction, Encapsulation In OOPs

Inheritance, Polymorphism, Abstraction, and Encapsulation are the four pillars of the Object Oriented Programming Language. The Object Oriented Language is basically a style of programming. It is characterized by the identification of the classes of the objects that are closely linked with the methods i.e. functions with which they are associated. OOPs is a kind of computer programming model. It organizes software design around the date or the objects. An Object is defined as the kind of data that has unique attributes and behavior.

The main focus of the Object Oriented Programming Language is on the objects that developers want to manipulate rather than the logic required to manipulate them. It is a well-known approach to computer Programming. They are well-suited for programs that are large, complex, and actively updated or maintained. Object Oriented Programming Language just includes programs for manufacturing and design. It is also used in Mobile applications. The well-known use of the Object Oriented Programming Language is for the Manufacturing System Simulation Software. 

The well-known organization of the Object Oriented Programming Language makes the method useful for collaborative development. It is used in the best manner in the collaborative development by the project works in the group among the members. The advantages of Object Oriented Programming Language are efficiency, scalability, reusability, and modularity. There are mainly four pillars of the OOPs concept. The detailed explanation of the OOPs Concept is as follows:

Inheritance: The Inheritance part of the Object Oriented Programming Language allows the user to define a class that inherits all the methods and attributes from the other class. There is a convention between the Child Class and the Parent class. The new class is referred to as the child class and the class from which the child class inherits is known as the Parent Class or the SuperClass. For any Monkey, there are a set of predefined properties that are common for all Monkeys and there are a set of properties that are specific to a particular Monkey.

Therefore, intuitively, we can say that all Monkeys inherit common features like tails, legs, eyes, etc. Therefore, in the object-oriented way of representing the Monkeys, we first declare a Monkey class with a set of properties that are common to all Monkeys. By doing this, we can avoid declaring these common properties in every Monkey which we create. Instead, we can simply inherit the Monkey class in all the Monkeys which we create. This is an example of how the concept of inheritance is implemented.

Polymorphism: Polymorphism refers to the fact that there are many existing forms. In programming, polymorphism is a feature that allows one interface to be used for a general class of actions. In the above concept of a Monkey and Mandrill, a Mandrill is inherently a Monkey. And also, if the Monkeys are further categorized into multiple categories like climbing monkeys, non-climbing monkeys, etc. the Monkeys also fit into the Climbing monkey’s category.

And also, if the animal class is further categorized into plant-eating animals and meat-eating animals, the Mandrill again comes into the plant-eating animal category. Therefore, the basic concept of the polymorphism of the OOPs concept is the ability of the same object to take multiple forms. There are two types of polymorphism:

  1. Compile Time Polymorphism: Compile-Time Polymorphism is also known as static polymorphism. This type of polymorphism is achieved by function overloading of operator overloading. The compile time polymorphism is mainly used to define multiple methods with different signatures. The compiler in the compile time polymorphism knows which method needs to be executed based on the method signatures.
  2. Runtime Polymorphism: Run-Time Polymorphism is also known as Dynamic Method Dispatch. The process of the Run Time Polymorphism is a function call to the overridden method that is resolved at Runtime. This is a well-known type of polymorphism. It is achieved by Method Overriding. In a java programming language, one can also upcast and downcast the objects. The core idea behind this concept is also polymorphism. The idea is that the object of the Monkey can have the value of the Mandrill because it inherits the features of the Monkey. Therefore, a parent object can also be initialized with the child properties if both the objects extend each other in the following way:

Monkey b = new Mandrill();

Abstraction: Abstraction in general means hiding. In the above scenario of the Monkey and Mandrill, let’s say there is a user who wants to see Mandrill climb. The user is simply interested in seeing the Mandrill Climb but not interested in how the Monkey is actually climbing. Therefore, in the above scenario where the user wishes to make it climb, he will simply call the climbing method by using mandrill.climb() where the mandrill is the object of the monkey mandrill.

Therefore, abstraction basically means the art of representing the essential features without concerning the background details. The abstraction is implemented through the use of interface and abstract classes.

Encapsulation: Now, we have defined the properties of the Monkey class, and the attributes which the monkeys have like color, tails, and legs can be initialized by creating an object of the monkey class. However, if we simply are able to change the properties of the monkey class just by the reference of the object, then the attributes lose the information by which it was initially initialized.

For example, if we initially created a mandrill with brown color by creating a constructor, any user with the instance of the object of the mandrill can change this color to yellow or green by simply referring to the attribute with the “this” keyword. For example, if a mandrill is created(born) with a brown color, it doesn’t change until the mandrill dies.

So, a user who is using simply shouldn’t be able to change the color as per his wish. The following is the implementation of the getters and setters for the above Monkey class.

This was about “What Is Inheritance, Polymorphism, Abstraction, Encapsulation In OOPs“. I hope this article may help you all a lot. Thank you for reading.

Also, read:

Author Profile

CS Electrical And ElectronicsChetu
Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking
Share Now

CS Electrical And Electronics

Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking