Python Inheritance – Learn to build relationship between classes
Inheritance is one of the foundational concepts of object-oriented programming, allowing new classes to inherit attributes and methods from existing ones. This promotes code reusability and establishes a natural hierarchy between classes, making the code more modular and easier to maintain. Inheritance is widely used in programming and is essential for building production-ready applications that are scalable and efficient. In this article, we will delve into the concept of Python inheritance, explore its benefits, and examine the different types of inheritance, such as single, multiple, and multilevel inheritance.
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. This can be represented as the given image.
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.
Code:
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.
Code:
issubclass(Apple, Fruit)
Output:
Types of Inheritance in Python
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. This is the simplest form of inheritance, where the subclass inherits all the attributes and methods of the parent class. It allows the subclass to reuse and extend the functionality of the parent class, promoting code reusability and reducing redundancy.
For example, if we have a Person class, a Student class can inherit from it, gaining all the properties and behaviors defined in the Person class while adding its own unique attributes and methods. Single inheritance helps create a clear and straightforward class hierarchy.
Code:
class Parent: def show(self): print("Parent method") class Child(Parent): def display(self): print("Child method") c = Child() c.display() c.show()
Output:
Parent method
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 multilevel inheritance, which means that there is no limit on the number of levels that you can inherit. In multilevel inheritance, a class inherits from a parent class, which in turn inherits from another parent class, forming a chain of inheritance. This allows for a more detailed and structured hierarchy, where each level can add specific functionalities.
For example, if you have a Vehicle class, a Car class can inherit from it, and a SportsCar class can further inherit from the Car class, each adding its own unique attributes and behaviors.
Code:
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()
Output:
B class
C class
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. To achieve this we can provide multiple classes separated by commas.
Code:
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()
Output:
B class
C class
D class
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. It is simple to understand with a diagram.
Code:
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()
Output:
A class
5. Hybrid Inheritance in Python
The term Hybrid describes that it is a mixture of more than one type. Hybrid inheritance is a combination of different types of inheritance.
Code:
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()
Output:
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.
Code:
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()
Output:
B class
100
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.
Code:
class A: def method(self): print("A class") class B(A): def method(self): print("B class") b = B() b.method()
Output:
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 –
Code:
def getDetails(): print("Name: Default") def getDetails(name): print("Name:", name) getDetails("Siri") getDetails()
Output:
Traceback (most recent call last):
File “C:\Users\Techvidvan\AppData\Local\Programs\Python\Python38-32\test.py”, line 8, in <module>
getDetails()
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.
Summary
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.