Python Inheritance

Introduction

In python, inheritance plays a vital role in making the code modular. In this article, we’ll see what is the actual concept of inheritance and how it works and what extra features can be appointed under this.

Before heading to inheritance, this article would be brief about classes and objects also.

Python classes and objects

Classes in python can be declared using the class keyword. The following example shows a simple structure of the python class.

class TUF:
  “docstring”
   def greet(self):
    print("Hello");

Objects can be used to create an instance of a class. The following example shows a simple structure of the object of a class.

#creating a class
class TUF:
    'docstring'
    def greet(self):
        print("Hello");

#creating an object
striver=TUF();
striver.greet();

Now we can begin with inheritance.

Inheritance

Let’s try understanding it with the help of a diagram.

Here we see that CAR, BIKE and TRUCK are all vehicles. Every type of vehicle has got its own usage but the main purpose would remain the same i.e. commute/transport. Hence, we can say that Vehicle is the superclass and CAR, BIKE and TRUCK are subclasses. 

From here we can try deriving a definition of inheritance as follows:

It is an object-oriented paradigm where a class has properties of its own type and also contains the properties of the class from which it is derived.

A much more clear definition would be The process in which a class inherits the properties of its super class is known as inheritance. The class which inherits is known as derived class/ sub class and the class from which the properties are inherited is known as base class/ super class.

Let’s see how inheritance is performed in python. The following code snippet shows how inheritance is performed in python

class baseClass:
  #code
class derivedClass(baseClass):
  #code

Inheritance Example

The following code is a simple example of inheritance

Code:

Python Code

class TUF:
    def __init__(self, name):
        self.name=name;
    def getName(self):
        return self.name;

class google(TUF):
    def display(self):
        print("I work at google");

striver=google("Raj");
print(striver.getName());
striver.display();

Output:

Raj
I work at google

Types of Inheritance

There are different types of inheritance in python which are listed as follows:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Let’s try understanding each type of inheritance using a simple diagram and code.

Single Inheritance

In single inheritance, there exists a single superclass and a single subclass. Sub-class inherits properties from this single superclass.

The following diagram explains the above statement.

The following code explains this type of inheritance:

Code:

Python Code

class A:
    def fun1(self):
        print("Super class A");
class B(A):
    def fun2(self):
        print("Sub class B");

obj=B();
obj.fun2();
obj.fun1();

Output:

Sub class B
Super class A

Multiple Inheritance

In this type of inheritance, there exists more than one superclass from which a subclass is inherited. This subclass inherits the properties of all the superclasses from which it was inherited.

The following diagram explains the above statement.

The following code explains this type of inheritance:

Code:

Python Code

#super class 1
class A:
    def fun1(self):
        print("Super class A");
        
#super class 2
class B():
    def fun2(self):
        print("Super class B");
        
#sub class
class C(A,B):
    def fun3(self):
        print("Derived from the following classes: ");
        self.fun1();
        self.fun2();

obj=C();
obj.fun3();

Output:

Derived from the following classes:
Super class A
Super class B

Multilevel Inheritance

In this type of inheritance, a subclass acts as a superclass for another subclass. This allows the final subclass to inherit all the properties of its ancestors.

The following diagram explains the above statement.

The following code explains this type of inheritance

Code:

Python Code

#Super class
class A:
    def fun1(self):
        print("Class A", end=" -> ");
        
#sub class
class B(A):
    def fun2(self):
        print("Class B", end=" -> ")
        
#sub-sub class
class C(B):
    def fun3(self):
        self.fun1();
        self.fun2();
        print("Class C");

obj=C();
obj.fun3();

Output:

Class A -> Class B -> Class C

Hierarchical Inheritance

In this type of inheritance, there exists only one superclass and many subclasses. All of these subclasses can inherit the properties of the superclass from which they were inherited.

The following diagram explains the above statement.

The following code explains this type of inheritance.

Code:

Python Code

#Super class
class A:
    def fun1(self):
        print("Super class A", end=" -> ")
        
#sub class 1
class B(A):
    def fun2(self):
        self.fun1();
        print("Sub class B");
        
#sub class 2
class C(A):
    def fun3(self):
        self.fun1();
        print("Sub class C");
        
#subclass 3
class D(A):
    def fun4(self):
        self.fun1();
        print("Sub class D");

obj1=B();
obj2=C();
obj3=D();
obj1.fun2();
obj2.fun3();
obj3.fun4();

Output:

Super class A -> Sub class B
Super class A -> Sub class C
Super class A -> Sub class D

Hybrid Inheritance

This type of inheritance is a mixture of all the inheritances. The following diagram explains this type of inheritance.

The following code explains this type of inheritance. 

Please note that the following code is a simpler version of hybrid inheritance. The diagram and code won’t be related to each other in this part.

Code:

Python Code

class A:
    def fun1(self):
        print("Class A");

class B(A):
    def fun2(self):
        print("Class B");
        
class C(A):
    def fun3(self):
        print("Class C");
 
class D(B,A):
    def fun4(self):
        print("Class D");

obj= D();
obj.fun1();
obj.fun2();

Output:

Class A
Class B

Special thanks to Yash Mishra for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this articleIf you want to suggest any improvement/correction in this article please mail us at [email protected]