Python Inheritance – Learn to build relationship between classes
Inheritance is one of the most basic concepts of Object-oriented programming.
It is widely used in programming and building production-ready applications.
In this article, we are going to understand what Python inheritance is and see the different types of inheritance.
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
What is Inheritance in Python?
Inheritance in object-oriented programming is inspired by the real-world inheritance in human beings.
We acquire some of the traits of our parents during birth.
In Python, inheritance is the capability of a class to pass some of its properties or methods to it’s derived class(child class).
With inheritance, we build a relationship between classes based on how they are derived.
For example, every car, bus, bikes are vehicles.
So we can build relationships between them and a car can inherit things from the vehicle.
Here, the Vehicle will be called parent or base class while the car, bus, and bike are its child or derived class.
Python Inheritance Example
To derive a class from another class we can simply use the name of the parent class in parentheses after the class name.
class Fruit: pass class Apple(Fruit): pass
Here, we have defined two classes Fruit and Apple.
The Apple class is derived from the Fruit class.
The pass statement is used to create an empty class.
Python has an in-built function issubclass() to check if a class is a subclass of another or not.
We can build different types of relationships between classes by the way they are inherited.
Python has 5 types of inheritance.
Let’s go through each of them.
1. Single Inheritance in Python
In single inheritance, a single class inherits from a class.
class Parent: def show(self): print("Parent method") class Child(Parent): def display(self): print("Child method") c = Child() c.display() c.show()
Here, we created an object of the Child class and we saw that from the Child object we can even call the method of Parent class.
This is the advantage of inheritance, we can reuse the code we have written.
2. Multilevel Inheritance in Python
Python supports the multilevel inheritance, which means that there is no limit on the number of levels that you can inherit.
class A: def methodA(self): print("A class") class B(A): def methodB(self): print("B class") class C(B): def methodC(self): print("C class") c = C() c.methodA() c.methodB() c.methodC()
Here, the object of C class can access the methods and properties of both A and B class because they were inherited from top to bottom.
But take note that the object of a B class cannot access methods of the C class.
3. Multiple Inheritance in Python
Till now, we were inheriting from only one class at a time.
In multiple inheritance, we will see that Python also allows us to inherit from more than one class.
class A: def methodA(self): print("A class") class B: def methodB(self): print("B class") class C: def methodC(self): print("C class") class D(A, B, C): def methodD(self): print("D class") d = D() d.methodA() d.methodB() d.methodC() d.methodD()
The object of a D class has directly inherited the properties and methods of A, B, and C classes.
4. Hierarchical Inheritance in Python
In a hierarchical inheritance, a class is inherited by more than one class.
class A: def methodA(self): print("A class") class B(A): def methodB(self): print("B class") class C(A): def methodC(self): print("C class") b = B() c = C() b.methodA() c.methodA()
5. Hybrid Inheritance in Python
The term Hybrid describes that it is a mixture of more than one type.
class A: def methodA(self): print("A class") class B(A): def methodB(self): print("B class") class C(A): def methodC(self): print("C class") class D(B,C): def methodD(self): print("D class") d = D() d.methodA()
Python Inheritance – super() function
When dealing with inheritance, super() is a very handy function.
super() is a proxy object which is used to refer to the parent object.
We can call super() method to access the properties or methods of the parent class.
class A: x=100 def methodA(self): print("A class") class B(A): def methodB(self): super().methodA() print("B class") print(super().x) b = B() b.methodB()
Python Overriding Methods
Method overriding is an important concept in object-oriented programming.
Method overriding allows us to redefine a method by overriding it.
For method overriding, we must satisfy two conditions:
- There should be a parent-child relationship between the classes.
- Inheritance is a must.
- The name of the method and the parameters should be the same in the base and derived class in order to override it.
What will happen when the method in the base and derived class are the same.
Let’s see an example of this.
class A: def method(self): print("A class") class B(A): def method(self): print("B class") b = B() b.method()
Here, the B class has inherited A class and we have the same function in both classes method().
Since the name and parameters are the same, the derived class overrides the method of the base class and when we call the method() the B class method is called.
This is known as method overriding.
Overloading Methods in Python
If you have some experience with object-oriented programming or other languages like C/C++ or Java then you might have used method overloading.
That is why it is important to understand that Python doesn’t support method overloading.
Let’s see the example –
def getDetails(): print("Name: Default") def getDetails(name): print("Name:", name) getDetails("Siri") getDetails()
Traceback (most recent call last):
File “C:\Users\Techvidvan\AppData\Local\Programs\Python\Python38-32\test.py”, line 8, in <module>
TypeError: getDetails() missing 1 required positional argument: ‘name’
As you can see, the getDetails(“Siri”) function got executed but the getDetails() was not executed.
This is because Python overwrites the function with the same name and the latter function is used.
In this article, we discussed a very important concept of object-oriented programming i.e. Python Inheritance.
We saw how we can pass properties or methods from one class to another using inheritance.
In this Python inheritance article, we have also covered its types, functions, and methods.