The perfect place for easy learning...

Python

×

Topics List


Python Inheritance





The inheritance is a very useful and powerful concept of object-oriented programming. Using the inheritance concept, we can use the existing features of one class in another class.

The inheritance is the process of acquiring the properties of one class to another class.

In inheritance, we use the terms like parent class, child class, base class, derived class, superclass, and subclass.

The Parent class is the class which provides features to another class. The parent class is also known as Base class or Superclass.

The Child class is the class which receives features from another class. The child class is also known as the Derived Class or Subclass.

In the inheritance, the child class acquires the features from its parent class. But the parent class never acquires the features from its child class.


There are five types of inheritances, and they are as follows.

  • Simple Inheritance (or) Single Inheritance
  • Multiple Inheritance
  • Multi-Level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

The following picture illustrates how various inheritances are implemented.

Inheritance Types | Python Inheritance Types

Creating a Child Class

In Python, we use the following general structure to create a child class from a parent class.

Syntax
class ChildClassName(ParentClassName):
    ChildClass implementation
    .
    .

Let's look at individual inheritance type with an example.

Simple Inheritance (or) Single Inheritance

In this type of inheritance, one child class derives from one parent class. Look at the following example code.

Example
class ParentClass:

    def feature_1(self):
        print('feature_1 from ParentClass is running...')

    def feature_2(self):
        print('feature_2 from ParentClass is running...')


class ChildClass(ParentClass):

    def feature_3(self):
        print('feature_3 from ChildClass is running...')

obj = ChildClass()
obj.feature_1()
obj.feature_2()
obj.feature_3()

When we run the above example code, it produces the following output.

Python Constructor

Multiple Inheritance

In this type of inheritance, one child class derives from two or more parent classes. Look at the following example code.

Example
class ParentClass_1:

    def feature_1(self):
        print('feature_1 from ParentClass_1 is running...')

class ParentClass_2:

    def feature_2(self):
        print('feature_2 from ParentClass_2 is running...')


class ChildClass(ParentClass_1, ParentClass_2):

    def feature_3(self):
        print('feature_3 from ChildClass is running...')

obj = ChildClass()
obj.feature_1()
obj.feature_2()
obj.feature_3()

When we run the above example code, it produces the following output.

Python Constructor

Multi-Level Inheritance

In this type of inheritance, the child class derives from a class which already derived from another class. Look at the following example code.

Example
class ParentClass:

    def feature_1(self):
        print('feature_1 from ParentClass is running...')

class ChildClass_1(ParentClass):

    def feature_2(self):
        print('feature_2 from ChildClass_1 is running...')


class ChildClass_2(ChildClass_1):

    def feature_3(self):
        print('feature_3 from ChildClass_2 is running...')

obj = ChildClass_2()
obj.feature_1()
obj.feature_2()
obj.feature_3()

When we run the above example code, it produces the following output.

Python Constructor

Hierarchical Inheritance

In this type of inheritance, two or more child classes derive from one parent class. Look at the following example code.

Example
class ParentClass_1:

    def feature_1(self):
        print('feature_1 from ParentClass_1 is running...')

class ParentClass_2:

    def feature_2(self):
        print('feature_2 from ParentClass_2 is running...')


class ChildClass(ParentClass_1, ParentClass_2):

    def feature_3(self):
        print('feature_3 from ChildClass is running...')

obj = ChildClass()
obj.feature_1()
obj.feature_2()
obj.feature_3()

When we run the above example code, it produces the following output.

Python Constructor

Hybrid Inheritance

The hybrid inheritance is the combination of more than one type of inheritance. We may use any combination as a single with multiple inheritances, multi-level with multiple inheritances, etc.,



Your ads here