Open In App

Inheritance in Python

Last Updated : 25 Mar, 2025
Suggest changes
Share
Like Article
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). This promotes code reuse, modularity, and a hierarchical class structure. In this article, we'll explore inheritance in Python.

Basic Example of Inheritance

Inheritance allows us to define a class that inherits all the methods and properties from another class.

Python
# Parent class class Animal: def __init__(self, name): self.name = name # Initialize the name attribute def speak(self): pass # Placeholder method to be overridden by child classes # Child class inheriting from Animal class Dog(Animal): def speak(self): return f"{self.name} barks!" # Override the speak method # Creating an instance of Dog dog = Dog("Buddy") print(dog.speak()) 

Output
Buddy barks! 

Explanation:

  • Animal is the parent class with an __init__ method and a speak method.
  • Dog is the child class that inherits from Animal.
  • The speak method is overridden in the Dog class to provide specific behavior.

Let's understand inheritance in detail:

Syntax for Inheritance

class ParentClass:

# Parent class code here
pass

class ChildClass(ParentClass):

# Child class code here
pass

Explanation of Python Inheritance Syntax

  1. Parent Class:
    • This is the base class from which other classes inherit.
    • It contains attributes and methods that the child class can reuse.
  2. Child Class:
    • This is the derived class that inherits from the parent class.
    • The syntax for inheritance is class ChildClass(ParentClass).
    • The child class automatically gets all attributes and methods of the parent class unless overridden.

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:

Python
# A Python program to demonstrate inheritance class Person(object): # Constructor def __init__(self, name, id): self.name = name self.id = id # To check if this person is an employee def Display(self): print(self.name, self.id) # Driver code emp = Person("Satyam", 102) # An Object of Person emp.Display() 

Output
Satyam 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:

Python
class Emp(Person): def Print(self): print("Emp class called") Emp_details = Emp("Mayank", 103) # calling parent class function Emp_details.Display() # Calling child class function Emp_details.Print() 

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.
  • Emp introduces an additional attribute, role, and also overrides the display method to print the role in addition to the name and id.

__init__() Function

__init__() function is a constructor method in Python. It initializes the object's state when the object is created. If the child class does not define its own __init__() method, it will automatically inherit the one from the parent class.

In the example above, the __init__() method in the Employee class ensures that both inherited and new attributes are properly initialized.

Python
# Parent Class: Person class Person: def __init__(self, name, idnumber): self.name = name self.idnumber = idnumber # Child Class: Employee class Employee(Person): def __init__(self, name, idnumber, salary, post): super().__init__(name, idnumber) # Calls Person's __init__() self.salary = salary self.post = post 

Explanation:

  • __init__() method in Person initializes name and idnumber.
  • __init__() method in Employee calls super().__init__(name, idnumber) to initialize the name and idnumber inherited from the Person class and adds salary and post.

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:

Python
# Parent Class: Person class Person: def __init__(self, name, idnumber): self.name = name self.idnumber = idnumber def display(self): print(self.name) print(self.idnumber) # Child Class: Employee class Employee(Person): def __init__(self, name, idnumber, salary, post): super().__init__(name, idnumber) # Using super() to call Person's __init__() self.salary = salary self.post = post 

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:

Python
# Parent Class: Person class Person: def __init__(self, name, idnumber): self.name = name self.idnumber = idnumber def display(self): print(self.name) print(self.idnumber) # Child Class: Employee class Employee(Person): def __init__(self, name, idnumber, salary, post): super().__init__(name, idnumber) self.salary = salary self.post = post 

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:

Python
# 1. Single Inheritance class Person: def __init__(self, name): self.name = name class Employee(Person): # Employee inherits from Person def __init__(self, name, salary): super().__init__(name) self.salary = salary # 2. Multiple Inheritance class Job: def __init__(self, salary): self.salary = salary class EmployeePersonJob(Employee, Job): # Inherits from both Employee and Job def __init__(self, name, salary): Employee.__init__(self, name, salary) # Initialize Employee Job.__init__(self, salary) # Initialize Job # 3. Multilevel Inheritance class Manager(EmployeePersonJob): # Inherits from EmployeePersonJob def __init__(self, name, salary, department): EmployeePersonJob.__init__(self, name, salary) # Explicitly initialize EmployeePersonJob self.department = department # 4. Hierarchical Inheritance class AssistantManager(EmployeePersonJob): # Inherits from EmployeePersonJob def __init__(self, name, salary, team_size): EmployeePersonJob.__init__(self, name, salary) # Explicitly initialize EmployeePersonJob self.team_size = team_size # 5. Hybrid Inheritance (Multiple + Multilevel) class SeniorManager(Manager, AssistantManager): # Inherits from both Manager and AssistantManager def __init__(self, name, salary, department, team_size): Manager.__init__(self, name, salary, department) # Initialize Manager AssistantManager.__init__(self, name, salary, team_size) # Initialize AssistantManager # Creating objects to show inheritance # Single Inheritance emp = Employee("John", 40000) print(emp.name, emp.salary) # Multiple Inheritance emp2 = EmployeePersonJob("Alice", 50000) print(emp2.name, emp2.salary) # Multilevel Inheritance mgr = Manager("Bob", 60000, "HR") print(mgr.name, mgr.salary, mgr.department) # Hierarchical Inheritance asst_mgr = AssistantManager("Charlie", 45000, 10) print(asst_mgr.name, asst_mgr.salary, asst_mgr.team_size) # Hybrid Inheritance sen_mgr = SeniorManager("David", 70000, "Finance", 20) print(sen_mgr.name, sen_mgr.salary, sen_mgr.department, sen_mgr.team_size) 

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


Next Article

Similar Reads

Article Tags :
Practice Tags :