5. Inheritance

It is a mechanism that allows you to create a hierarchy of classes that share a set of properties and methods by deriving a class from another class. Inheritance is the capability of one class to derive or inherit the properties from another class.

  • It specifies that the child object acquires all the properties and behaviors of the parent object.
  • It provides re-usability of the code.

Python Inheritance Terminologies

  • Superclass ors base class or parent class
    • The class from which attributes and methods are acquired.
  • Subclass or derived class or child class
    • The class which inherits the members from superclass.
# Python Inheritance Syntax

Class BaseClass:
    pass
Class DerivedClass(BaseClass):
    pass

5.1 Types of Inheritance

# Single Inheritance
Parent1 -> Child1
A --> B

# Multi – Level Inheritance
Parent1 -> Child1 -> Child2 
A --> B --> C

# Multiple Inheritance
Parent1 -> Child2 <- Parent2
A,B --> C

# Hierarchical Inheritance
Parent1 -> Child1
Parent1 -> Child2

5.2 Single Inheritance

Single Inheritance is the simplest form of inheritance where a single child class is derived from a single parent class.

# Base class
class Parent:
    def func1(self):
        print("Parent class")
 
# Derived class
class Child(Parent):
    def func2(self):
        print("Child class")

# Driver's code
object = Child()
object.func1()
object.func2()

5.3 Multiple Inheritance

In multiple inheritance, a single child class is inherited from two or more parent classes. It means the child class has access to all the parent classes' methods and attributes.

class Mother:
    def mother(self):
        print('Mother')

class Father:
    def father(self):
        print("Father")

class Son(Mother, Father):
    def parents(self):
        self.mother()
        self.father()

s1 = Son()
s1.parents()

5.4 Multilevel Inheritance

In multilevel inheritance, we go beyond just a parent-child relation. We introduce grandchildren, great-grandchildren, grandparents, etc.

We have seen only two levels of inheritance with a superior parent class/es and a derived class/es, but here we can have multiple levels where the parent class/es itself is derived from another class/es.

class Parent:
    def func1(self):
        print("Parent class")
 
class Child(Parent):
    def func2(self):
        print("Child class")

class Grandchild(Child):
    def func3(self):
        print("Grand child class")

object = Grandchild()
object.func1()
object.func2()
object.func3()

5.5 Hierarchical Inheritance

Hierarchical Inheritance is the right opposite of multiple inheritance. It means that, there are multiple derived child classes from a single parent class.

class Parent:
    def func1(self):
        print("Parent class") 

class Child1(Parent):
    def func2(self):
        print("Child 1")
 
class Child2(Parent):
    def func3(self):
        print("Child 2")

object1 = Child1()
object2 = Child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()

5.6 Hybrid Inheritance

Hybrid inheritance is a combination of more than one type of inheritance.

class Parent:
    def func1(self):
        print("Parent class")

class Child1(Parent):
    def func2(self):
        print("Child 1")

class Child2(Parent):
    def func3(self):
        print("Child 2")

class GrandChild(Child1, Child2):
    def func4(self):
        print("GrandChild")

object1 = Child1()
object2 = Child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()

obj3 = GrandChild()
obj3.func4()