The term “inheritance” is frequently used to refer to the passing of some essential functions from one generation to the next. Like parent classes, there are child and base classes. We create classes through inheritance by deriving them from other pre-existing classes. The parent/base classes, by which the child classes acquire their methods and attributes, are the already existent classes.
Advantages of Inheritance in Python
- Modular Codebase: This makes the codebase more modular by dividing it into modules, making it much easier to understand. Every class we construct, in this case, will become a distinct module that may be independently inherited through one or more classes.
- Code reuse: The child class incorporates all of the parent class’s attributes and functionalities into its own. By avoiding the need to rewrite them, this adheres to modularity concepts and saves a lot of time and coding work.
- Lower Maintenance and Development Costs: By only making changes to a base class, all derived classes will be auto-updated.
Disadvantages of Inheritance in Python
- Loads more classes that are more than necessary since those rely on one another, slowing the execution speed.
- Tightly Linked Classes: This describes a situation in which child classes could not run without first specifying their parent classes, even though parent classes may execute separately.
Types of Inheritance in Python
Let us look at various categories of inheritance because now we have all the information we need to understand how inheritance works in Python.
Single Inheritance in Python
It is the most accessible form of inheritance under which only one parent class corresponds to one child class. It also goes by the name simple inheritance because of its openness.
First, we create a parent class, “class P,” then define a function of this class f(). To show the output of this parent class, we use a print() function. We provide the argument ‘Good Morning’ to the print method. After this, we create a child class named “class C” This class is inherited from the parent class P. Then, define a function f1() in child class and utilize the print() method. This print statement displays the text ‘Good Evening’. In the driver code section, we declared an object. Then, called a parent method by child object and finally employed the child method.
Multiple Inheritance in Python
One child class inherits via two or more two-parent classes when there are multiple inheritances. It indicates that the parent classes’ functions and attributes are accessible to the child class.
The child class, meanwhile, executes the functions of the first parent in order of referencing if two parents share similar functions. The method resolution order function may determine which class’s methods run first.
We begin by creating the first parent class “class P_1”. Then, we define a function f1() and use the print() function. We provide the string “Cherry” to represent as output. Further, we executed the second parent class “class P_2”, defined its function f2(), then applied a print() method. Here, we pass “Orange” as the parameter of this method.
Now, let us create a third parent class named “class P_3”. We also called a function f3() for the third parent class and utilized a print statement. After all these classes, we execute a child class “class C(P_1, P_2, P_3)” as this combines all three parent classes. Moreover, we have defined a function f4() of the child class. The print command will be applied to represent the output “Banana” of the child class. In the driver code section, we created first an object, then we called the P_1 method by a child class, the P_2 method by child instead of P_3, and lastly, we will call the child method.
The child class accesses itself as well followed by the first parent class, referred to before a second parent class. In this code, the second parent class calls before a third parent class, fulfilling the second parent’s role instead of the third parents. Finally, it makes a stop at any generated objects.
Multi-Level Inheritance in Python
Until now, we have seen only two degrees of inheritance, with a greater parent class or classes and a derived class or classes. However, we may have several levels in this case, with both the parent class or classes themselves deriving from some other class or classes.
We started by declaring our first level as “class_GP”. Next, we defined its function a_1() and utilized a print() method to print the result. Now, we declared a send level “class P(GP)”. We also initialized a function a_2() of this class. To denote output “Green,” we utilize the print statement. After that, we create a third level “class C(P)” and employ a function as a_3(). We show the outcome “Yellow” by utilizing the print() method. Let us start the code in the driver section. Here, we have to create an object. Then, the 1st level is called by the 3rd level, the 2nd level is called by using the 3rd level, and the 3rd level is called the 3rd level at the end.
Hierarchical Inheritance in Python
Multiple inheritance’s counterparts are hierarchical inheritance. It indicates that such a single-parent class has various derived child classes.
In this example, we would make a parent class “class A”. Along with this, we invoked a function e1(). Then, we passed a parameter “Lemon” in the print () function to show the output of this class. After this, we created another class known as the first child class, “class C_1(A)”, and defined its method as e2(). Here, we will represent the output “Beetroot” by the use of the print command. Further, we executed the second child class “class C_2(A)”. Next, we defined a function e3() of this child class. We have provided “spinach” as a print() method argument.
We would create two objects. We will call the parent method the first child object. Then, child 1 calls by its methods. The parent method is called using child 2. In the end, child 2 calls by its method.
Conclusion
In this article, I have explained “what is inheritance in python” along with some advantages and disadvantages of using inheritance. After that, I have discussed some types of python inheritances that differ based on the distribution or placement of parent and child functions. We also execute examples of each type and explain the codes used.
from https://ift.tt/m4DR8LY
0 Comments