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.

Before moving ahead, check out Python objects – The important concept of OOPs.

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.python inheritance - relationship between classes

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:

True

Types of Inheritance in Pythontypes of python inheritance

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.single inheritance in python

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:

Child method
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 the multilevel inheritance, which means that there is no limit on the number of levels that you can inherit. We can achieve multilevel inheritance by inheriting one class from another which then is inherited from another class.multilevel inheritance in python

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:

A class
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.multiple python inheritance

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:

A class
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.hierarchical python 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")

b = B()
c = C()

b.methodA()
c.methodA()

Output:

A class
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.hybrid python 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:

A class

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:

A class
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:

B class

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:

Name: Siri
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.

I hope this TechVidvan’s Python Inheritance article was helpful to you. If you have any doubts, ask them in the comment section.

Happy Learning!!

2 Responses

  1. Gigiusb says:

    The diagram on hybrid inheritance is wrong. C and D should be swapped.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.