- What is Inheritance in Python?
- Python Inheritance Syntax
- Inheritance example
- #Output
- Behind the code
- Types of Inheritance
- Single Inheritance In Python
- Single Inheritance Syntax
- Example
- Output
- Behind the code
- Multiple Inheritance In Python
- Multiple Inheritance Syntax
- Example
- Output
- Behind the code
- Multilevel Inheritance In Python
- Multilevel Inheritance Syntax
- Multilevel Inheritance Example
- Output
- Behind the code
- Hierarchical Inheritance In Python
- Hierarchical Inheritance Syntax
- Hierarchical Inheritance Example
- Output
- Hybrid Inheritance In Python
- Syntax
- Hybrid Inheritance Example
- Output
- Python Super function
- Benefits of using the super() function
- Super Function with Single inheritance
- Example
- Output
- Break the code
- Super Function with Multiple inheritances
- Example
- Output
- Break the code
- Method Overriding
- #Output
- Behind the Code
- What is Method Resolution Order in Python?
- Example
- Output
- Break the code
Inheritance is one of the features of Object-Oriented Programming. According to this feature, the child class can inherit or access the public properties and methods of the parent class. In Python, we do not have access modifiers to separate public or private properties of a parent class, this means, in Python, the child class can inherit all the properties and methods of its parent class. The parent class and child class terminologies are also referred to as base class and drive class. This Python tutorial will teach
Python inheritance
, method overloading, method overriding, inheritance types, and Method Resolution order.
What is Inheritance in Python?
As we know that inheritance is one of the features of OOP and it is used too often. According to the inheritance concept we can use the properties of one class in another class, and these properties could be its class methods and attributes. In Inheritance, we require at least two classes base and derived . The base class will be a normal class but the derived class will have some modification in its syntax. We could also consider base class as a parent class and derived class as a child class.
Python Inheritance Syntax
To inherit the base class we pass the base class name as an argument to the derived class.
Inheritance example
#Output
Behind the code
In the above example, we have defined 2 classes Student and Output , here Student is the base class whereas Output is the derived class which means the Output class can inherit the properties of the Student class. In Student class we have defined all the attributes ( roll , m1 , m2 , and ea ) inside the get () method and access those attributes in the Output class display() method where we make the total . In the above program we did not create the object of Student class only with the help of Output object student we accessed the Student get() method to take input from the user and use those inputs in display() method. This inherits property used by Output class in which it accessed the Student class attributes and get () method prove that in python class can follow the inheritance property. The main objective of inheritance in Python or any other programming language is the reusability of code, by inheriting the parent class we do not need to write the same logic for the child class. This type of technique comes very handy in big projects where many sub-sections require the same properties as the main section.
Types of Inheritance
The type of inheritance in Python depends upon the relationship between the chile and parent classes. There are five types of inheritance in Python.
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Now let’s discuss all these inheritance types one by one with examples
Single Inheritance In Python
In Single Inheritance, there is one parent class and one child class, and the child class directly inherits from the single parent class.
Single Inheritance Syntax
Example
Let’s create two classes Car and BMW, and perform single inheritance where the BMW class inherit the Car class.
Output
Behind the code
In the above example, we perform the single inheritance. The Car was the base class that was inherited by the single drive class BMW.
Multiple Inheritance In Python
In multiple inheritance, a single child class can inherit from two or more than two parents classes.
Multiple Inheritance Syntax
Example
Let’s write a script in Python that demonstrates Python Multiple Inheritance, for multiple inheritance, there must be at least two parent classes.
Output
Behind the code
In this multiple inheritance example, the child class BMW inherited properties from two parent classes RTO and Car. Because of multiple inheritances, bmw object is able to access the car_wheel() and car_current_owner() methods which belong to the parent class.
Multilevel Inheritance In Python
In Multilevel Inheritance, the child class can be the parent class of another class. Let’s say we have three classes, A, B, and C. Where A is the parent class of B and B is the parent class of C. Making B the child and C the grandchild of class A. Here A does not have any parent class which makes a SuperClass.
Multilevel Inheritance Syntax
In multilevel inheritance, the last child ultimately inherits the properties of all the above parents.
Multilevel Inheritance Example
Output
Behind the code
In this example, we perform multilevel inheritance using Car , BMW , and Showroom classes. The attribute access in Multilevel inheritance flow from down to up, or from child to parent. Similarly in the above example, the Showroom class is able to inherit BMW , as well as Car properties.
Hierarchical Inheritance In Python
In Hierarchical inheritance, we have one parent class that is inherited by two or more two-child classes.
Hierarchical Inheritance Syntax
Hierarchical Inheritance Example
Output
Hybrid Inheritance In Python
In Hybrid inheritance, the relationship between different classes is a combination of all the inheritance we have discussed in the above examples.
Syntax
Hybrid Inheritance Example
Output
Python Super function
When a child class inherits a parent class, the child class becomes the subclass of the parent class, where the parent class is known as a superclass. When we want to call or access a parent class method inside a child class, we can either use the object or the name of the parent class. But Python provides a super() function, which creates a proxy or temporary object of the parent class so we can access the parent class method inside the child class.
Benefits of using the super() function
- With the super function, we do not need to use the parent class name while accessing the parent methods.
- It can work with both single and multiple inheritance.
- It comes very handy during function or method overloading.
Super Function with Single inheritance
In single inheritance, the super() function directly refers to the superclass of the child method that is the parent class.
Example
Output
Break the code
In the above example when we created the object of BMW() class bmw, it automatically call its __init__() function. And inside its __init__() function we have called its superclass’s ( Car ) __init__() function using super().__init__() statement.
Super Function with Multiple inheritances
In multiple inheritances, the super function refers to the superclass according to the Method Resolution Order.
Example
Output
Break the code
In the above example the super().__init__() statement of the BMW class call the Car’s class, and the super().__init__() statement inside the Car’s class call the RTO’s class __init__() method.
Method Overriding
When a child class inherits a parent class, it inherits the parent’s all properties such as methods and attributes. But if any method of the parent class does not satisfy the child class, the child class can have a method of itself with the same method name as the parent class. This redefining of the same method in the child class is known as method overriding .
Let’s understand it with an example:
#Output
Behind the Code
In the above code when we create the Cat classe’s object tom and we pass a name “tom” along with the class cat(“tom”) this will invoke the Animal class constructor that is __init__() because the cat class does not have any __init__ method. But when we use the tom object and call the speak method then the Cat class’s speak() method gets invoked not the Animal class speak() method why so? because the interpreter first looks at all the methods of Cat class if the Cat class did not have the speak method then it would call the Animal speak method. The calling of methods by the objects depends upon the Method Resolution Order now let’s discuss what it is?
What is Method Resolution Order in Python?
The Method Resolution Order is an order followed by Python to look up for the methods or attributes of a class object. The order generally goes from down to up, which means from child to parent. The MRO plays a very crucial role in multiple inheritance and method overriding. The Method Resolution Order of a Class can be seen using the mro() method.
Example
Let’s see an example of multiple inheritance and how the order of method accessing followed by it.
Output
Break the code
In the above example, the C inherits from B and A , that’s why when we call the method() method on class object c it calls the method() inside the Class C because it has high priority. The output of C.mro() list shows the order in which the c object should look for the methods. While initializing the class C, we set inheritance as ( B, A) , and because Python read from left to right B has the higher priority than A .