Open In App

Inheritance in Python

Last Updated : 24 Jul, 2025
Comments
Improve
Suggest changes
336 Likes
Like
Report

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class). In this article, we'll explore inheritance in Python.

Syntax for Inheritance

class ParentClass:

# Parent class code here
pass

class ChildClass(ParentClass):

# Child class code here
pass

Explanation of Syntax

Parent Class:

  • This is the base class from which other classes inherit.
  • It contains attributes and methods that the child class can reuse.

Child Class:

  • This is the derived class that inherits from the parent class.
  • Syntax for inheritance is class ChildClass(ParentClass).
  • Child class automatically gets all attributes and methods of the parent class unless overridden.

Why do we need Inheritance in Python

  • Promotes code reusability by sharing attributes and methods across classes.
  • Models real-world hierarchies like Animal → Dog or Person → Employee.
  • Simplifies maintenance through centralized updates in parent classes.
  • Enables method overriding for customized subclass behavior.
  • Supports scalable, extensible design using polymorphism.

Creating a Parent Class

In object-oriented programming, a parent class (also known as a base class) defines common attributes and methods that can be inherited by other classes. These attributes and methods serve as the foundation for the child classes. By using inheritance, child classes can access and extend the functionality provided by the parent class.

Here's an example where Person is the parent class:


Output
Elon 102

Explanation

  • The Person class has two attributes: name and id. These are set when an object of the class is created.
  • The display method prints the name and id of the person.

Creating a Child Class

A child class (also known as a subclass) is a class that inherits properties and methods from its parent class. The child class can also introduce additional attributes and methods, or even override the ones inherited from the parent.

In this case, Emp is the child class that inherits from the Person class:

Explanation:

  • Emp class inherits the name and id attributes and the display method from the Person class.
  • __init__ method in Emp calls super().__init__(name, id) to invoke the constructor of the Person class and initialize the inherited attributes.

super() Function

super() function is used to call the parent class’s methods. In particular, it is commonly used in the child class’s __init__() method to initialize inherited attributes. This way, the child class can leverage the functionality of the parent class.

Example:

Explanation:

  • The super() function is used inside the __init__() method of Employee to call the constructor of Person and initialize the inherited attributes (name and idnumber).
  • This ensures that the parent class functionality is reused without needing to rewrite the code in the child class.

Add Properties

Once inheritance is established, both the parent and child classes can have their own properties. Properties are attributes that belong to a class and are used to store data.

Example:

Explanation:

  • Person class has properties name and idnumber.
  • Employee class adds properties salary and post.
  • The properties are initialized when an object is created, and they represent the specific data related to the Person and Employee.

Types of Python Inheritance

  1. Single Inheritance: A child class inherits from one parent class.
  2. Multiple Inheritance: A child class inherits from more than one parent class.
  3. Multilevel Inheritance: A class is derived from a class which is also derived from another class.
  4. Hierarchical Inheritance: Multiple classes inherit from a single parent class.
  5. Hybrid Inheritance: A combination of more than one type of inheritance.

Example:


Output
John 40000
Alice 50000
Bob 60000 HR
Charlie 45000 10
David 70000 Finance 20

Explanation:

  1. Single Inheritance: Employee inherits from Person, adding a salary attribute.
  2. Multiple Inheritance: EmployeePersonJob inherits from both Employee and Job, allowing access to both name and salary.
  3. Multilevel Inheritance: Manager inherits from EmployeePersonJob, which already includes Employee and Job.
  4. Hierarchical Inheritance: AssistantManager also inherits from EmployeePersonJob, demonstrating multiple child classes inheriting from the same parent.
  5. Hybrid Inheritance: SeniorManager inherits from both Manager (multilevel) and AssistantManager (hierarchical), combining two inheritance types.

For more details please read this article: Types of inheritance in Python


Inheritance in Python
Visit Course explore course icon
Next Article
Article Tags :
Practice Tags :

Similar Reads