Inheritance in Python

Inheritance at a glance!

In the world of Object-Oriented Programming (OOP), Inheritance refers to the mechanism of the capability of a class to derive or extend the properties from another class in the run. This property enables the derived class to acquire the properties or traits of the base class.

Inheritance is considered one of the most important aspects of OOP because it serves the feature of reusability, thus making the piece of code more reliable.

Python Inheritance
Python Inheritance

Benefits of Inheritance

  • Inheritance depicts relationships that resemble real-world scenarios.
  • It provides the feature of re-usability which allows the user to add more features to the derived class without altering it.
  • If a class Y inherits from class X, then automatically all the sub-classes of Y would inherit from class X.

Basic Terminologies of Inheritance

  1. Subclass/Derived class: It is a class that inherits the properties from another class (usually the base class).
  2. Superclass/Base class: It is the class from which other subclasses are derived.
  3. A derived class usually derives/inherits/extends the base class.

Python Inheritance Syntax

class SuperClassName:
  Body of Super class


class DerivedClass_Name(SuperClass):
  Body of derived class

Python Inheritance Example

Let’s dive into the world of inheritance in Python with simple examples.

Step 1: Create a Base class